假设我们有一个像这样的对象数组-
const arr = [ {"Date":"2014","Amount1":90,"Amount2":800}, {"Date":"2015","Amount1":110,"Amount2":300}, {"Date":"2016","Amount1":3000,"Amount2":500} ];
我们需要编写一个JavaScript函数,该函数接受一个这样的数组并将该数组映射到包含数组而不是对象的另一个数组。
因此,最终数组应如下所示:
const output = [ ['2014', 90, 800], ['2015', 110, 300], ['2016', 3000, 500] ];
为此的代码将是-
const arr = [ {"Date":"2014","Amount1":90,"Amount2":800}, {"Date":"2015","Amount1":110,"Amount2":300}, {"Date":"2016","Amount1":3000,"Amount2":500} ]; const arrify = (arr = []) => { const res = []; const { length: l } = arr; for(let i = 0; i < l; i++){ const obj = arr[i]; const subArr = Object.values(obj); res.push(subArr); }; return res; }; console.log(arrify(arr));
输出结果
控制台中的输出将是-
[ [ '2014', 90, 800 ], [ '2015', 110, 300 ], [ '2016', 3000, 500 ] ]