Convert an array of objects to array of the objects' values
我正试图转换这个数组
1 2 3 4 5 6 | let orders = [ { amount: '100', user: 'admin', date: 'March 6, 2019' }, { amount: '120', user: 'admin', date: 'March 6, 2019' }, { amount: '80', user: 'admin', date: 'March 7, 2019' }, { amount: '200', user: 'admin', date: 'March 7, 2019' }, ]; |
像这样的东西
1 2 3 4 5 6 | orders = [ ['100', 'admin', 'March 6, 2019'], ['120', 'admin', 'March 6, 2019'], ['80', 'admin', 'March 7, 2019'], ['200', 'admin', 'March 7, 2019'], ]; |
我已经读到
1 2 3 | let newOrders = orders.forEach(order => { return Object.values(order); }); |
我不知道我所做的是否正确,我对JavaScript还不是很熟悉。请帮帮我。
由于无法保证
1 2 3 4 5 6 7 8 9 10 | const data = [ { amount: '100', user: 'admin', date: 'March 6, 2019' }, { amount: '120', user: 'admin', date: 'March 6, 2019' }, { amount: '80', user: 'admin', date: 'March 7, 2019' }, { amount: '200', user: 'admin', date: 'March 7, 2019' } ]; const result = data.map(({ amount, user, date }) => [amount, user, date]); console.log(result); |
1 | .as-console-wrapper { max-height: 100% !important; top: 0; } |
不能保证枚举对象属性的顺序(ref)。最简单的解决方案是按所需顺序显式指定键:
1 | let result = orders.map(order => [order.amount, order.user, order.date]); |
使用
1 2 3 4 5 6 7 8 | let orders = [ { amount: '100', user: 'admin', date: 'March 6, 2019' }, { amount: '120', user: 'admin', date: 'March 6, 2019' }, { amount: '80', user: 'admin', date: 'March 7, 2019' }, { amount: '200', user: 'admin', date: 'March 7, 2019' }, ]; console.log(orders.map(({amount,user,date})=>[amount,user,date])) |
使用
1 2 3 4 5 6 7 | let orders = [ { amount: '100', user: 'admin', date: 'March 6, 2019' }, { amount: '120', user: 'admin', date: 'March 6, 2019' }, { amount: '80', user: 'admin', date: 'March 7, 2019' }, { amount: '200', user: 'admin', date: 'March 7, 2019' }, ]; console.log(orders.map(e=>Object.values(e))) |
只需使用
1 2 3 4 5 6 7 8 9 10 | let orders = [ { amount: '100', user: 'admin', date: 'March 6, 2019' }, { amount: '120', user: 'admin', date: 'March 6, 2019' }, { amount: '80', user: 'admin', date: 'March 7, 2019' }, { amount: '200', user: 'admin', date: 'March 7, 2019' }, ]; const result = orders.map(Object.values); console.log(result) |
1 2 3 4 5 6 7 8 9 10 | let orders = [ { amount: '100', user: 'admin', date: 'March 6, 2019' }, { amount: '120', user: 'admin', date: 'March 6, 2019' }, { amount: '80', user: 'admin', date: 'March 7, 2019' }, { amount: '200', user: 'admin', date: 'March 7, 2019' }, ]; const result = orders.map(Object.values); console.log(result) |
您可以尝试以下操作:
41 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | let orders = [{ amount: '100', user: 'admin', date: 'March 6, 2019' }, { amount: '120', user: 'admin', date: 'March 6, 2019' }, { amount: '80', user: 'admin', date: 'March 7, 2019' }, { amount: '200', user: 'admin', date: 'March 7, 2019' }, ]; let array = []; //initializing array orders.forEach((element) => { //using array function for call back for (var j in element) { //looping through each element of array array.push(element[j]); //pushing each value of object present inside the orders } }); console.log(array); //array is ready |
一个更强大的解决方案,如果您有许多实例,其中这些
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | let orders = [ { amount: '100', user: 'admin', date: 'March 6, 2019' }, { amount: '120', user: 'admin', date: 'March 6, 2019' }, { amount: '80', user: 'admin', date: 'March 7, 2019' }, { amount: '200', user: 'admin', date: 'March 7, 2019' }, ]; // option 1 let propsToArray = function(...keys) { return function(obj) { return keys.map(key => obj[key]); } }; // option 2 // propsToArray = (...keys) => (obj) => keys.map(key => obj[key]); // resulting function let orderToArray = propsToArray("amount","user","date"); console.log(orders.map(orderToArray)); |