2017-02-25 52 views

回答

0

几种方法达到的效果

var data = [ 
 
    [5], 
 
    [27], 
 
    [39], 
 
    [1001] 
 
]; 
 

 
// Use map method which iterate over the array and within the 
 
// callback return the new array element which is first element 
 
// from the inner array, this won't work if inner array includes 
 
// more than one element 
 
console.log(
 
    data.map(function(v) { 
 
    return v[0]; 
 
    }) 
 
) 
 

 
// by concatenating the inner arrays by providing the array of 
 
// elements as argument using `apply` method 
 
console.log(
 
    [].concat.apply([], data) 
 
) 
 

 
// or by using reduce method which concatenate array 
 
// elements within the callback 
 
console.log(
 
    data.reduce(function(arr, e) { 
 
    return arr.concat(e); 
 
    }) 
 
)

相关问题