JavaScript 中的对象数组到数组数组
javascriptweb developmentfront end technologyobject oriented programming更新于 2024/1/5 22:04:17
假设,我们有一个像这样的对象数组 −
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 ] ]
相关文章
有用资源
javascript 参考教程 - 该教程包含有关 javascript 的更多信息:https://www.cainiaomax.com/javascript/

