Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am trying to get the minimum value from an array without changing the way the the value are aligned.
function removeSmallest(numbers){
var min=Math.min.apply(null,numbers);
var indexvalue=numbers.indexOf(min);
delete numbers[indexvalue];
return numbers;
}
Two issues:
Math.min.apply(null, numbers) should be Math.min.apply(Math, numbers) (note Math instead of null)
Normally, delete is not the right tool for arrays (see this question's answers for details; use splice to remove array entries if you're going to modify the array in place:
numbers.splice(indexvalue, 1);
You might also consider creating and returning a new array instead of mutating and returning the same array you received.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 19 days ago.
Improve this question
Below is the screen of my VScode, I am declaring arr1 as an array of numbers and later reversing it.Input
The problem is that on output the 1st paragraph is showing the reversed arrays instead of original one. What is the exact problem?
Output
Try this:
let reverse = Array.from(arr1).reverse()
As reverse() method modifies the original array (arr1), then you need to create a new array with Array.from(): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse
Reverse is a mutating method, so both arr1 and reverse reference the same array. You'll want to do something like const reverse = [...arr1].reverse() instead to ensure that you aren't mutating the original array.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I am using this method to find the frequency of words, however i am a bit confused with the whole code.
Here is the code:
function check(){
var word = document.querySelector('textarea').value.split(" ");
frequency ={};
word.forEach(function(i){
console.log(i)
if(!frequency[i]){
frequency[i] = 0;
}
frequency[i]+=1
})
console.log(frequency)
}
<textarea></textarea>
<button onclick = 'check()'>check</button>
I just wonder what does the i stand for and what does the frequency[i].
Could someone explain to me a little bit because I think this code is not quite friendly for me, the beginner.
foreach iterates over array, and as paramter has function into which is passed as parameter actual element of array. So i in this function is actual element of 'word' array.
for object frequency is frequency[i] the i'th element. At start, this object is empty, so frequency[i] will be undefined, but in foreach loop you filling this object with some values so in next iterations there may by some values
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I want to compare two arrays
array1=["123',"456"];
array2=[{id":"001",name:"prashant"},{id:"123",name:"jhh"},{id:"123444",name"baak"},{id:"456",name"sxs"}];
my objective is to extract the objects from array2 whose ids match the values in array1.
Can someone help me with the optimal solution?
First of all your second array (array2) is syntactically invalid.
You can try with Array.prototype.filter() and Array.prototype.includes()
var array1=["123","456"];
var array2 = [{id:"001",name:"prashant"},{id:"123",name:"jhh"},{id:"123444",name:"baak"},{id:"456",name:"sxs"}];
var res = array2.filter(i => array1.includes(i.id));
console.log(res);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
So I have a function "profile" that returns an array.
And I have a function "TMYW" whose results I want to add to the array that profile returns. So I tried something like this:
profile(m.hours).push(TMYW(m.hours));
Interestingly this whole statement returns the integer 3.
Just curious as to why this happens. Obviously it is easy to get the desired result by doing something like this:
var b = profile(m.hours);
b.push(TMYW(m.hours));
See the following regarding the use of Array.prototype.push():
The push() method adds one or more elements to the end of an array and returns the new length of the array.
So in your case:
var b = profile(m.hours); // returns an array
b.push(TMYW(m.hours)); // returns the new length of b
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Consider the code below:
// Get Index of object based on id value
var arrayPosition = userListData.map(function (arrayItem) {
return arrayItem.username;
}).indexOf(thisUserName);
Why is it that arrayItem = userListData[0], userListData[1], userListData[2]... ?
In general, they do not.
That is just what the map function is designed to do.
callback —
Function that produces an element of the new Array, taking three arguments:
currentValue — The current element being processed in the array.
etc