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

184
Views
How to turn object with array into array?

I'm trying to turn the values of an array inside an object to a normal array.

Right now I'm doing something like this in my AJAX success function:

var obj = data
              
console.log(obj)

var array = Object.keys(obj)
 .map(function(key) {
   return obj[key];
 });

console.log(array)

This is what I get in my console:

{jaar: Array(2)}
 jaar: Array(2)
  0: YEAR(Scan): "2020"
   [[Prototype]]: Object
  1: YEAR(Scan): "2021"
   [[Prototype]]: Object
  length: 2
  [[Prototype]]: Array(0)
  [[Prototype]]: Object

I want to have something simply as this

["2020", "2021"]

How can I achieve this and what am I doing wrong right now?

console.log(obj) at the start gives this output in the console:

jaar: Array(2)
0: {YEAR(Scan): '2020'}
1: {YEAR(Scan): '2021'}
length: 2
[[Prototype]]: Array(0)
[[Prototype]]: Object ```
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

If you have a plain object like const obj = { x: 10, y: 20 } and you want to have an array of its values, you can simply use Object.values(obj) that will return you [10, 20].

Seems like your object has more complex structure.

If you know for sure, any value of your object is a value or array, like const obj2 = { x: 10, y: [20, 30, 40], z: [50, 60]} you can flatten it.

Object.values(obj2).flat() will return you [10, 20, 30, 40, 50, 60].

For more complex structures you need to have more complex handlers.

about 4 years ago · Juan Pablo Isaza Report

0

jaar is already an array of objects. You can simply use .map() and get the respective property value from each array element.

let ans =jaar.map(x => x['YEAR(Scan)']);
about 4 years ago · Juan Pablo Isaza Report

0

Other answer provides the must succinct code (using .map) - to make the the smallest change to your existing code, change:

You can read the YEAR(Scan) property like so:

return obj[key]["YEAR(Scan)"];

Updated code:

var obj = 
  [ 
    { "YEAR(Scan)":"2020" },
    { "YEAR(Scan)":"2021" }
  ]
  

console.log(obj)

var array = Object.keys(obj)
 .map(function(key) {
   return obj[key]["YEAR(Scan)"];
 });

console.log(array)

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!