How to get values from a nested array in javascript? [duplicate] - javascript

This question already has answers here:
Merge/flatten an array of arrays
(84 answers)
Closed 4 years ago.
I have an array like let a = [[[1,2], [3,[1,2,3]]], [2,3]] and want to access the elements using a method/way to return the values like : 12312323 or [1,2,3,1,2,3,2,3]
how can I approach the solution in javascript/nodeJS?
thanks

You can use Array.flat()
let a = [[[1,2], [3,[1,2,3]]], [2,3]]
let op = a.flat(Infinity)
console.log(op)
You can check browser compatibility here compatibility

Related

How to manipulate an array, eliminating the empty items? [duplicate]

This question already has answers here:
How can I remove a specific item from an array in JavaScript?
(142 answers)
How to remove item from array by value? [duplicate]
(37 answers)
Closed 2 months ago.
I have an array which has some empty items.
const array = ["a","","c","","e","f","g"]
I am trying to eliminate empty items and leave only the items having string. I mean manipulating the array as:
array = ["a","c","e","f","g"]
There are many alternative like array.map, array.filter, array.slice, array.splice.. What is the most costless and recommended way of doing this?
As far as I know most cost effective way is to use the array filter method:
const array = ["a","","c","","e","f","g"];
const results = array.filter(element => {
return element !== '';
});
console.log(results);

how to get values in dropdown from print multiple arrays from an array in javascript or typescript? [duplicate]

This question already has answers here:
Merge/flatten an array of arrays
(84 answers)
Closed 3 months ago.
let's see I have an array like let filterData= [[1,2,3],[x,y],[z,10]] Have to make it in single line or in single array as an output: this.filterData= [ 1,2,3,x,y,z,10];how can I achieve this in Javascript or typescript language.
anyone have a solution of it..please suggest
`if (this.data && this.data.length > 0) {
this.filteredData.push(this.data[index].value);
this.filteredData.push(this.filteredData);
console.log(this.filteredData)
}`
Just using Array.flat() can do it
let filterData= [[1,2,3],['x','y'],['z',10]]
filterData = filterData.flat()
console.log(filterData)
Try like this
let filterData= [[1,2,3],["x","y"],["z",10]];
let array=[];
filterData.map((x)=>{x.forEach((m)=>{array.push(m)});})
console.log(array);

JavaScript How to declare 16 length array with default 0 value quick? [duplicate]

This question already has answers here:
Most efficient way to create a zero filled JavaScript array?
(45 answers)
Closed 3 years ago.
let array1 = Array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
how to make like this array with shortest way ? is it possible to make shorter way ?
thank you for your answers.
You can use the .fill() method:
let array1 = new Array(16).fill(0);
As its name suggests, it fills the array with some desired value.

Swap values of keys in JSON array [duplicate]

This question already has answers here:
Swap value of two properties on object(s)
(3 answers)
Closed 8 years ago.
I have the following JSON. I need to swap SortId
Like I have this,
[{"CategoryId":1,"Name":"Worktable","SortId":1}
,{"CategoryId":2,"Name":"Bf ","SortId":2}]
After swaping their 'SortId' I need
[{"CategoryId":1,"Name":"Worktable","SortId":2}
,{"CategoryId":2,"Name":"Bf ","SortId":1}]
Please tell me how to do it through JavaScript.
var tmp = a[0].SortId;
a[0].SortId = a[1].SortId;
a[1].SortId = tmp;
jsFiddle

Deleting an element from JS associative array [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
JavaScript Array Delete Elements
How is this done?
If I have the following array defined:
var myArr = [];
myArr[id1] = {prop1: prop1Value, prop2:prop2Value};
myArr[id2] = {prop1: prop1Value, prop2:prop2Value};
//etc
I wish to delete myArr[id1]
Perhaps you mean that the numerical indices after the deleted element aren't updated... in which case you'll need to use splice:
myArr.splice(id1, 1);

Categories

Resources