Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

143
Views
Covert key-value javascript object into array format

I am trying to access java script object which is available in the below format:

[
  {'To':'A','From':'X','Weight':5},
  {'To':'A','From':'Y','Weight':7},
  {'To':'A','From':'Z','Weight':6},
  {'To':'B','From':'X','Weight':2},
  {'To':'B','From':'Y','Weight':9},
  {'To':'B','From':'Z','Weight':4}
]

How can I access above object to create array like below ?

[
  [ 'A', 'X', 5 ],
  [ 'A', 'Y', 7 ],
  [ 'A', 'Z', 6 ],
  [ 'B', 'X', 2 ],
  [ 'B', 'Y', 9 ],
  [ 'B', 'Z', 4 ]
]
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Well, you can use array method .map()

let arr = [
  {'To':'A','From':'Y','Weight':7},
  {'To':'A','From':'Z','Weight':6}
]

let result = arr.map(Object.values)

console.log(result)

about 4 years ago · Juan Pablo Isaza Report

0

You can

  • Use Array.map() and Object.values():

    const arrOfArrs = arr.map( Object.values );
    
  • Use Lodash's _.map() and _.values() in the same manner:

    const _ = require('lodash');
    . . .
    const arrOfArrs = _.map( arr , _.values );
    
    

You should, however, be aware that the order in which an object's properties are iterated over (and hence returned) is not guaranteed in any way by the Ecmascript/Javascript standard. It can vary from Javascript implementation to implementation, and can even change from execution to execution. The most common order in which things are returned is insertion order, but that's not guaranteed.

about 4 years ago · Juan Pablo Isaza Report

0

Use a map function:

const l = [
  {'To':'A','From':'X','Weight':5},
  {'To':'A','From':'Y','Weight':7},
  {'To':'A','From':'Z','Weight':6},
  {'To':'B','From':'X','Weight':2},
  {'To':'B','From':'Y','Weight':9},
  {'To':'B','From':'Z','Weight':4}
]

const newArray = l.map(el => [el.To, el.From, el.Weight])
console.log(newArray);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!