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

219
Views
Split an array in javascript based on the key for dataPoints

I am working on a personal project and I want to display the graph for which i need to prepare the datapoints. The response I have is

{
  "size":[
    {
      "timestamp":"1641329889000",
      "size":12345,
      "fees":123456,
      "cost":168
    },
    {
      "timestamp":"1641387032",
      "size":456789,
      "fees":4567891,
      "cost":249
    },
    {
      "timestamp":"1641435786",
      "size":98765,
      "fees":987654,
      "cost":987
    },
  ]
}

I want the output should be like this

{
  "size":{
    "timestamp": ["1641329889000","1641387032","1641435786"],
    "size": [12345,456789,98765],
    "fees": [123456,4567891,987654],
    "cost": [168,249,987]
  }
}

Can anyone help me out with this

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

0

Using .reduce(), you can use a generic function without any hardcoded datapoint names.

const data={size:[{timestamp:"1641329889000",size:12345,fees:123456,cost:168},{timestamp:"1641387032",size:456789,fees:4567891,cost:249},{timestamp:"1641435786",size:98765,fees:987654,cost:987}]};

const result = data.size.reduce((acc, item) => {
  const keys = Object.keys(item);
  return keys.reduce((keyAcc, key) => ({
    ...keyAcc,
    [key]: acc[key] ? [...acc[key], item[key]] : [item[key]]
  }), {});
}, [])

console.log({ size: result});

about 4 years ago · Juan Pablo Isaza Report

0

let obj = {
  "size":[
    {
      "timestamp":"1641329889000",
      "size":12345,
      "fees":123456,
      "cost":168
    },
    {
      "timestamp":"1641387032",
      "size":456789,
      "fees":4567891,
      "cost":249
    },
    {
      "timestamp":"1641435786",
      "size":98765,
      "fees":987654,
      "cost":987
    },
  ]
}

let timestamps = [];
let sizes = [];
let fees = [];
let costs = [];

obj.size.foreach( ( data ) => {
   timestamps.push( data.timestamp );
   sizes.push( data.size);
   fees.push( data.fees );
   costs.push( data.cost );
}

let size = { timestamp: timestamps, size : sizes, fees : fees, cost : costs }

let outputObj = { size : size };

console.log( outputObj );
about 4 years ago · Juan Pablo Isaza Report

0

Sure it's simple. :D

{
 "size":{
    "timestamp": size.map(item => item.timestamp),
    "size": size.map(item => item.size),
    "fees": size.map(item => item.fees),
    "cost": size.map(item => item.cost),
  }
}
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!