关于javascript:将对象数组转换为对象值的数组

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'],
];

我已经读到Objects.values()返回数组中的值,所以我尝试通过使用forEach()和对数组中的每个项使用Object.values来迭代order数组。

1
2
3
let newOrders = orders.forEach(order => {
  return Object.values(order);
});

我不知道我所做的是否正确,我对JavaScript还不是很熟悉。请帮帮我。


由于无法保证Object.values()返回的数组中的值的顺序,因此应考虑使用.map()进行一些对象破坏。然后可以在单独的变量中提取对象属性,并显式地按所需的顺序返回它们。

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]);


使用destructuring。如果输出中需要(对象的)属性排序,则使用此选项

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]))

使用mapObject.values从对象中获取值。这不能保证输出中的顺序与对象中的顺序相同。请参阅

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)))


只需使用orders.map(Object.values)

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)


您可以尝试以下操作:

4

map将返回一个新数组,而forEach只对数组的每个元素进行回调


1
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


一个更强大的解决方案,如果您有许多实例,其中这些struct类对象具有不同的顺序/键,则非常有用。作为一种函数方法,propsToArray将一系列键作为单独的参数,并返回一个对对象执行所需转换的函数。

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));