物件陣列到 JavaScript 中的陣列陣列
假設我們有一個像這樣的物件陣列 −
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 ] ]
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
安卓
Python
C 程式設計
C++
C#
MongoDB
MySQL
JavaScript
PHP