ShEp ShEp - 5 months ago 9
Javascript Question

Javascript - Remove a particular type(string,int) of element from an array

With

array1
:

array1 = [["hello",1],["bye",2]]


By the way
array1[0]
return
["hello",1]
and not
["hello"]


How can I get (in a new, or the same array) :

array2 = [[1],[2]]


Note my
array1
have (in reality) a lot of elements.

Thanks.

Answer

Use Array#map

var array1 = [
  ["hello", 1],
  ["bye", 2]
];
var array2 = array1.map(function(item) {
  return [item[1]];
});
console.log(array2);

Or to retrieve only Numbers

var array1 = [
  ["hello", 1],
  ["bye", 2]
];
var array2 = array1.map(function(item) {
  return item.filter(function(val){ return isNaN(val); });
});
console.log(array2);

Comments