43

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

8 Answers 8

57

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

Sign up to request clarification or add additional context in comments.

1 Comment

It doesn't seem to be mentioned in your Object destructuring link, but the order is preserved because variable names are relevant and should match the property names. const result = data.map(({ date, amount, user }) => [amount, user, date]); works just as well even though the variable definitions has been modified. This link helped me understand.
22

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

Comments

22

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

6 Comments

Even simpler orders.map(Object.values)
It's relevant because it means your code could return ['100', 'admin', 'March 6, 2019'] for an order and ['admin', 80', 'March 7, 2019'] for another.
The spec does not require the order, but apparently all major browsers follow the order anyway.
@JollyJoker False. I seem to remember that chrome has certain problems, especially when it comes to objects the have numerical keys. It orders it as 1, 111, 1245, 13 instead of 1, 13, 111, 1245.
@I.Am.A.Guy That's the order in the spec. See stackoverflow.com/a/30919039/7126740
|
6

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)

Comments

2

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)

Comments

1

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

Comments

0

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.