This question already has answers here:
Using Javascript to sort an array of numeric arrays
(5 answers)
Closed 9 months ago.
Create a program that will allow you to enter a number of integers to input. Then input values for those integers and store them into an array(Array A). After that, create another array(Array B) that will store the doubled value of elements from the first array. Display the two arrays. Display also the ascending order of the first array and the descending order of the second array.
look into array mapping (and arrow functions to make life easier)
e.g. var newNumbers = numbers.map(n => n*2)
Will return another array with all the elements of the numbers array doubled.
also look into sort() and reverse()
e.g. newNumbers.sort(function(a, b){return a-b}).reverse();
Will sort the array and reverse its order.
You need this comparison thing in the sort() because of they way numbers are processed with sort()
This question already has answers here:
How to map more than one property from an array of objects [duplicate]
(7 answers)
Closed 1 year ago.
How can I take (in JS) all the values inside this nested object except for the last two elements (scenario and total). here an image of the object I have:
I need an array equal to this one, but without "scenario" and "total" in the 3 nested objects inside.
Use map function
array.map(item => {
delete item.scenario;
delete item.total;
return item;
});
This question already has answers here:
Why Doesn't Array Concatenation Work in Javascript? [closed]
(3 answers)
Closed 2 years ago.
I am getting below like values at runtime. Below is for only example. I will have same array structure at runtime. I want to merge them.
let finalSearchResult =[];
data [{"a":1000,"a":1000001,"a":10000002,"D":5000000}]
data [{"P":1000,"Q":1000001,"R":10000002,"S":5000000}]
finalSearchResult.concat(finalSearchResult,data);
but its not working. When I am printing finalSearchResult its coming as null.
The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.
please refer to:
Array.prototype.concat documentation
Therefore:
finalSearchResult = finalSearchResult.concat(data);
Will be the correct way to concat the arrays.
This question already has answers here:
Is there a way to use Array.splice in javascript with the third parameter as an array?
(7 answers)
Closed 4 years ago.
i have 2 2D array, and want to merge a[0] with b[0]
var a=[["a","b","c"],["d","e","f"]];
var b=[["1","2","3"],["4","5","6"]];
console.log(a[0].splice(1,2,b[0]));
its return ["a",["1","2","3"]]
i need to archive ["a","1","2","3"] for a[0]
can any body show how ?
From docs, syntax of Array.splice is
array.splice(start[, deleteCount[, item1[, item2[, ...]]]])
As you can see, you can add more than 1 element to the array e.g. arr.splice(0,0,4,5) where you are adding 2 values (4 & 5) into array. With b[0] as 3rd parameter, you are adding the whole array at a particular index. To add individual value, you need to spread the values of array. You can use spread syntax for that. With that arr.splice(0,0,...[1,2]) will become arr.splice(0,0,1,2)
var a=[["a","b","c"],["d","e","f"]];
var b=[["1","2","3"],["4","5","6"]];
a[0].splice(1,2,...b[0])
console.log(a[0]);
You can use spread syntax
Spread syntax allows an iterable to expand in places where 0+
arguments are expected.
var a=[["a","b","c"],["d","e","f"]];
var b=[["1","2","3"],["4","5","6"]];
//if you are trying to achieve all elements in single array then
var result = [...a[0], ...b[0]];
console.log(result);
//if you are trying to achieve single element from first array and all from other then
var result = [...a[0][0], ...b[0]];
console.log(result);
Good article on Spread syntax
This question already has answers here:
remove objects from array by object property
(15 answers)
Closed 6 years ago.
I have an array of objects such as Id:1, Name:Greg
Now I need to remove the object with an Id of 5.
I just need the existing array with the item removed, not a new array.
I don't want to use an external library to do this.
(The suggested duplicate answer is for deleting a number of objects at once, which is not what I want to do.)
What I would like to do is call
remove(theArray, theObject);
and it would remove it from the array.
Look into array.splice method. You can pass it the index if object and 1 for delete count. This modifies the original array.
The splice() method changes the content of an array by removing
existing elements and/or adding new elements.
array.splice(start, deleteCount[, item1[, item2[, ...]]])
var index = items.indexOf(itemToFind);
items.splice(index, 1);