I have the following array in javascript:
DATA[
[ 3, "Name1" ],
[ 5, "Name2" ],
[ 10, "Name3" ],
[ 55, "Name4" ]
]
I would like to convert it to:
DATA[ "Name1", "Name2", "Name3", "Name4" ]
Is it possible in javascript?
Thanks
I have the following array in javascript:
DATA[
[ 3, "Name1" ],
[ 5, "Name2" ],
[ 10, "Name3" ],
[ 55, "Name4" ]
]
I would like to convert it to:
DATA[ "Name1", "Name2", "Name3", "Name4" ]
Is it possible in javascript?
Thanks
There's a few ways to do this, but here's one:
for(var i=0;i<DATA.length;i++){
DATA[i] = DATA[i][1];//only grab the 2nd item in the array
}
Simple use-case for Array.prototype.map
let data = [
[3, "Name1"],
[5, "Name2"],
[10, "Name3"],
[55, "Name4"]
];
data = data.map(arr => arr[1]);
console.log(data);
Simply use a map and grab the second item of each child array to build the new array.
const DATA = [
[ 3, "Name1" ],
[ 5, "Name2" ],
[ 10, "Name3" ],
[ 55, "Name4" ]
]
const newArr = DATA.map(i => i[1])
console.log(newArr)
try this:
let data = [
[3, "Name1"],
[5, "Name2"],
[10, "Name3"],
[55, "Name4"]
];
const result = data.map(([k, v])=>v);
console.log(result);
Try this :
var data =[[ 3, "Name1" ],[ 5, "Name2" ],[ 10, "Name3" ],[ 55, "Name4" ]];
var newdata=[];
data.forEach(function(element){
if(Array.isArray(element)){
newdata.push(element[1]);
}
});
console.log(newdata);