问题
I am trying to convert this array
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' },
];
to something like this
orders = [
['100', 'admin', 'March 6, 2019'],
['120', 'admin', 'March 6, 2019'],
['80', 'admin', 'March 7, 2019'],
['200', 'admin', 'March 7, 2019'],
];
and I have read that Objects.values()
returns the values in an array, so I tried to iterate through the order
array by using forEach()
and using the Object.values
on each item in the array.
let newOrders = orders.forEach(order => {
return Object.values(order);
});
I don't know if what I am doing is right and I am new to Javascript. Please help me.
回答1:
As order of values in array returned by Object.values() isn't guaranteed, you should consider use of .map() with some Object Destructuring. You can then extract object properties in separate variables and return them in desired order explicitly.
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);
.as-console-wrapper { max-height: 100% !important; top: 0; }
回答2:
The order in which the object's properties are enumerated is not guaranteed (ref). The simplest solution is to explicitly specify the keys in the desired order:
let result = orders.map(order => [order.amount, order.user, order.date]);
回答3:
Using destructuring
. Use this if property ordering (of the object) is required in the output
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]))
Use map
and Object.values
to get the values from the objects. This does not assure the order in the output will be the same as in the object Refer this
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)))
回答4:
Simply use orders.map(Object.values)
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)
回答5:
you can try this:
orders.map((order) => Object.values(order));
map
will return you a new array, while forEach
just do callback on each element of array
回答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' },
];
const result = orders.map(Object.values);
console.log(result)
回答7:
A more robust solution, useful if you have many instances where you have these struct
-like objects with different orders/keys. A functional approach, propsToArray
takes a series of keys as individual parameters and returns a function which performs the desired transformation on the objects.
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));
回答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'
},
];
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
来源:https://stackoverflow.com/questions/55041867/convert-an-array-of-objects-to-array-of-the-objects-values