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

274
Views
how to access array inside object without keys

I have a JSON data which look like this:

 {
  "chart":[
     [
        1627776011000,
        28
     ],
     [
        1627776371000,
        38
     ],
     [
        1627776731000,
        48
     ],
     ...
    ]
}

I want to use this data in Chart.js where 1627776011000 ... are x axis values and 28 ... are y axis values.

I know I can do it with an array push

const myArr = JSON.parse(dataJSON);

const data = []; 
const labels = [];

myArr.chart.forEach((item) => {
    labels.push(item[0]);
    data.push(item[1]);
});

but could there be a better way, for example with a map?

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

0

You can use .reduce() in as follows:

const myArr = {
  "chart":[
     [
        1627776011000,
        28
     ],
     [
        1627776371000,
        38
     ],
     [
        1627776731000,
        48
     ]
    ]
};

const [labels, data] = myArr.chart.reduce((acc,cur) => [
    [...acc[0], cur[0]],
    [...acc[1],cur[1]]],
[[],[]]);
console.log( labels, data );

about 4 years ago · Juan Pablo Isaza Report

0

You can do it as two calls to map():

const labels = myArr.chart.map(el => el[0]);
const data = myArr.chart.map(el => el[1]);
about 4 years ago · Juan Pablo Isaza Report

0

This seems to me like an invented problem.

If your data points are just x and y values, there's nothing wrong with saying the first array element is x and the second is y.

There are some people who would argue and say you should make it an Object with properties like xValue and yValue.

However, any upsides or downsides in either case would be marginal. You could imagine someone saying the Object version is more readable. The Object version is harder to optimize by the javascript runtime (V8 at least): https://v8.dev/blog/elements-kinds

Unless you have measured it, and it matters, it doesn't matter.

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!