Skip to content Skip to sidebar Skip to footer

How To Extract Value From An Array Of Array

I have an array of array stored. I need to extract the particular value from this arrays. e.g allarray contain the list of arrays allarray= [Array[3],Array[3],Array[3]] are three a

Solution 1:

If I understand you correctly, your array looks like this

var allarray = [["a1","b1","c1"],["a2","b2","c2"],["a3","b3","c3"]];

To get c1, c2, and c3 you could just do this

var c1 = allarray[0][2], c2 = allarray[1][2], c3 = allarray[2][2];

or you could do a loop to put all of the cs in a single array of its own

var cs = [];
for(var i = 0; i < allarray.length; i++) {
  cs.push(allarray[i][2]);
}

Solution 2:

This is what the Array.prototype.map function is for:

var arr = [["a1","b1","c1"],["a2","b2","c2"],["a3","b3","c3"]];

var theValues = arr.map(function(inner) {returninner[2]});

alert(theValues.join(', '));

Solution 3:

Can try using map(). Example:

var allarray = [["a1","b1","c1"],["a2","b2","c2"],["a3","b3","c3"]],
    index = 2;
allarray.map(function(val, ind){
    document.write(allarray[ind][index] + '<br />');
});
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

Solution 4:

var allarray = [
    ["a1", "b1", "c1"],
    ["a2", "b2", "c2"],
    ["a3", "b3", "c3"]
],
    num = 2;

//one by one
allarray.forEach(function( arr ) {
    alert( arr[ num ] );
});

//or all at oncealert( allarray.map(function( arr ) { return arr[ num ]; }).join(',') );

Solution 5:

var arrOfArr=[['a1','b1','c1'],['a2','b2','c2'],['a3','b3','c3']];

var cVals=arrOfArr.map(function(element,index){
      return arrOfArr[index][2];
});
alert(cVals);

http://jsfiddle.net/3uaugbem/

Post a Comment for "How To Extract Value From An Array Of Array"