2017-12-08 8 views
-1

にオブジェクトを変換する私はこのようなオブジェクトの配列の配列がありますJavascriptが配列

var array = [ 
    [ 
    { 
     name: 'a', 
     value: 1, 
     where: '1st array' 
    }, 
    { 
     name: 'b', 
     value: 2, 
     where: '1st array' 
    } 
    ], 
    [ 
    { 
     name: 'a', 
     value: 1, 
     where: '2nd array' 
    }, 
    { 
     name: 'b', 
     value: 2, 
     where: '2nd array' 
    } 
    ] 
] 

をそして私はこれにそれを変換したい:

[ 
    ['a', 1, '1st array'], 
    ['b', 2, '1st array'], 
    ['a', 1, '2nd array'], 
    ['b', 2, '2nd array'] 
] 

が、これはarray.map()方法を使用して行うことができます?私は1000個以上のオブジェクト/配列があり、変換する必要がありますので、私はforforが効率的でないかもしれないと思っています。

+0

実行時間は、あなたが最速で両方のソリューションとテストを書くことができ重要な場合。私は 'のために'あなたが考えるかもしれないよりよい運賃だと思う。 –

+0

ここをクリックしてください:https://stackoverflow.com/questions/20881213/converting-json-object-into-javascript-array – OlafW

+0

1000要素をループすることは、どのようにしても遅くなることはありません。 – JJJ

答えて

1

内部配列の破壊割り当てを使用できます。

var array = [[{ name: 'a', value: 1, where: '1st array' }, { name: 'b', value: 2, where: '1st array' }], [{ name: 'a', value: 1, where: '2nd array' }, { name: 'b', value: 2, where: '2nd array' }]], 
 
    result = array.reduce(
 
     (r, a) => r.concat(a.map(({ name, value, where }) => ([name, value, where]))), 
 
     [] 
 
    ); 
 
    
 
console.log(result);

0

これを達成する別の方法があります。 values=Object.values(array)のような値を取得し、単純なfor loopを使用してこれらの値を反復処理します。

var array = [ 
 
    [ 
 
    { 
 
     name: 'a', 
 
     value: 1, 
 
     where: '1st array' 
 
    }, 
 
    { 
 
     name: 'b', 
 
     value: 2, 
 
     where: '1st array' 
 
    } 
 
    ], 
 
    [ 
 
    { 
 
     name: 'a', 
 
     value: 1, 
 
     where: '2nd array' 
 
    }, 
 
    { 
 
     name: 'b', 
 
     value: 2, 
 
     where: '2nd array' 
 
    } 
 
    ] 
 
] 
 
var outPut =[] 
 
var values=Object.values(array); 
 
//console.log(values); 
 
for(var i=0;i<values.length;i++){ 
 
for(var j=0;j<values[i].length;j++){ 
 
    //console.log(values[i][j]); 
 
    outPut.push(Object.values(values[i][j])); 
 
    
 
} 
 
} 
 
console.log(outPut);

2

Object.values方法で、このためのループのための簡単な使用、あなたが道を以下にこれを実行する必要がある配列

var arr = [ 
 
    [ 
 
    { 
 
     name: 'a', 
 
     value: 1, 
 
     where: '1st array' 
 
    }, 
 
    { 
 
     name: 'b', 
 
     value: 2, 
 
     where: '1st array' 
 
    } 
 
    ], 
 
    [ 
 
    { 
 
     name: 'a', 
 
     value: 1, 
 
     where: '2nd array' 
 
    }, 
 
    { 
 
     name: 'b', 
 
     value: 2, 
 
     where: '2nd array' 
 
    } 
 
    ] 
 
]; 
 
var newArr = []; 
 
for(let i in arr){ 
 
    for(let j in arr[i]){ 
 
    newArr.push(Object.values(arr[i][j])); 
 
    } 
 
} 
 
console.log(newArr);

0

などの値を取得します。あなたはそれを改善することができます静的にしたくない場合はy動的にすることができます。しかし、それは長いコードになります... :) var object1、object2;

array[0].forEach(function(element,index){ 
    obejct1[index] = Object.values(element);  

}); 

array[1].forEach(function(element,index){ 
    obejct2[index] = Object.values(element);  

}); 

object1[0].concat(object1[1],object2[0],object1[1]);