I have the following array set:
myArr = [
[red, 1],
[blue, 2],
[yellow, 3],
[geen, 4]
];
Is there an operator/method in which I can return an array of just the 0 index item? So end result would be:
myArr2 = [red, blue, yellow, green]
I have the following array set:
myArr = [
[red, 1],
[blue, 2],
[yellow, 3],
[geen, 4]
];
Is there an operator/method in which I can return an array of just the 0 index item? So end result would be:
myArr2 = [red, blue, yellow, green]
You could use Array#map().
ES6
var myArr = [
['red', 1],
['blue', 2],
['yellow', 3],
['geen', 4]
];
document.write(myArr.map(a => a[0]));
ES5
var myArr = [
['red', 1],
['blue', 2],
['yellow', 3],
['geen', 4]
];
document.write(myArr.map(function (a) { return a[0]; }));
Working Example using map:
var a = [
["red", 1],
["blue", 2],
["yellow", 3],
["geen", 4]
];
var b = a.map(function(el) {
return el[0];
});
you can do it like that
for(var i=0;i<myArr.length;i++){
myArr2.push(myArr[i][0]);
}
console.log(myArr2); /// prints red, blue, yellow, green