将对象数组转换为对象值数组

时间:2019-03-07 10:44:07

标签: javascript arrays object javascript-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' },
];

像这样

orders = [
  ['100', 'admin', 'March 6, 2019'],
  ['120', 'admin', 'March 6, 2019'],
  ['80', 'admin', 'March 7, 2019'],
  ['200', 'admin', 'March 7, 2019'],
];

我已经读到Objects.values()返回数组中的值,因此我尝试通过使用order并在每个数组上使用forEach()来遍历Object.values数组数组中的项。

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

我不知道我在做什么是否正确,并且对Javascript不熟悉。请帮助我。

8 个答案:

答案 0 :(得分:45)

由于不能保证Object.values()返回的数组中值的顺序,因此应考虑将.map()与某些Object Destructuring一起使用。然后,您可以将对象属性提取到单独的变量中,并以所需顺序显式返回它们。

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

答案 1 :(得分:22)

不能保证枚举对象属性的顺序(ref)。最简单的解决方案是按所需顺序显式指定键:

let result = orders.map(order => [order.amount, order.user, order.date]);

答案 2 :(得分:19)

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

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从对象中获取值。 这不能确保输出中的顺序与对象中的顺序相同 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)))

答案 3 :(得分:3)

只需使用ret = func_to_test(data) # lets assume this returns list of bytes objects ret = [st_frame(value) for value in ret] self.assertEqual(ret, target)

orders.map(Object.values)

答案 4 :(得分:1)

您可以尝试以下操作:

orders.map((order) => Object.values(order));

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

答案 5 :(得分:1)

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)

答案 6 :(得分:0)

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

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

答案 7 :(得分:0)

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