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

194
Views
How to convert an array from one schema to another in Javascript?

I have a JSON array that looks like this:

[{
   "day":"all",
   "client_first":"3",
   "client_second":"2",
   "client_third":"3"
},
{
   "day":"monday",
   "client_first":"2",
   "client_second":"2",
   "client_third":"2"
}]

I would like to transform the above into the following

[{
    label: 'all',
    data: [3,2,3]
},
{
    label: 'monday',
    data: [2,2,2]
}]

Thanks in advance

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Given input:

const input = [{
   "day":"all",
   "client_first":"3",
   "client_second":"2",
   "client_third":"3"
},
{
   "day":"monday",
   "client_first":"2",
   "client_second":"2",
   "client_third":"2"
}];

Looks like the semantics of what you want to do align very well with Array.map.

For each values in the input array, map it to an object such that label is set by day (key rename), and put in to data the values of the other entries. To get the entries (key,value pairs) we use Object.entries. We Array.filter to get those with key !== 'day'. We want an array of the values, so we map the filtered entries (key,value pairs) to only the values. The input is string, but looks like you want it as numbers, so we convert using Number function

const output = input.map(obj => ({
    label: obj.day,
    data: Object.entries(obj).filter(([key]) => key !== 'day').map(([, val]) => Number(val)),
}));
about 4 years ago · Juan Pablo Isaza Report

0

In one line of code:
arr.map(({day,...clients})=>({label:day,data:[Object.values(clients).map(Number)]})):

  • looping over array and getting the day using Destructuring from object and clients using rest_parameters

let arr = [{
   "day":"all",
   "client_first":"3",
   "client_second":"2",
   "client_third":"3"
},
{
   "day":"monday",
   "client_first":"2",
   "client_second":"2",
   "client_third":"2"
}];
console.log(arr.map(({day,...clients})=>({label:day,data:[Object.values(clients).map(Number)]})))

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!