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

83
Views
Create array data structure from nested object of arrays in JS

I have a data structure as follows:

UPDATED: to have more than one object in innerValues

 const data = {
  "dates": [
    "2015-09-09T00:00:00",
    "2015-09-09T00:10:00",
    "2015-09-09T00:20:00",
  ],
  "innerValues": [
    {
    "name": "Name_1",
    "value": [
      104,
      105,
      107,
    ]
  },
  {
    "name": "Name_2",
    "value": [
      656,
      777,
      145,
    ]
  }],
}

I would like to create an output like:

const outPut = [
   ["2015-09-09T00:00:00", 'Name_1', 104 ],
   ["2015-09-09T00:10:00", 'Name_1', 105 ],
   ["2015-09-09T00:20:00", 'Name_1', 107 ],
   ["2015-09-09T00:00:00", 'Name_2', 104 ],
   ["2015-09-09T00:10:00", 'Name_2', 105 ],
   ["2015-09-09T00:20:00", 'Name_2', 107 ]
]

So far I have this, I know I could do this with forEach etc also - but as an example.

const m = data.dates;
let arr = [];

for (var i = 0; i < m.length; i++) {
  arr[i] = new Array(m[i]);
}

console.log(arr); 

This gives:

0: ['2021-09-09T00:00:00']
1: ['2021-09-09T00:10:00']
2: ['2021-09-09T00:20:00']

Which is where I want to start, however if I map over inner.values and try to create an new array from that, it does not return three separate arrays but one.e.g

const newArray = x.forEach(inner => console.log(new Array(inner)))

Output

0: (3) [104, 105, 107]

How could I achieve the above desired structure.

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

0

You can use reduce and map to loop over innerValues and their values

const dates = data.dates
const newArr = data.innerValues.reduce((acc, cur, i) => {
  acc.push(cur.value.map((value, i) => ([dates[i], cur.name, value])));
  return acc;
}, []).flat()
console.log(newArr)
<script>
  const data = {
    "dates": [
      "2015-09-09T00:00:00",
      "2015-09-09T00:10:00",
      "2015-09-09T00:20:00",
    ],
    "innerValues": [{
        "name": "Name_1",
        "value": [
          104,
          105,
          107,
        ]
      },
      {
        "name": "Name_2",
        "value": [
          656,
          777,
          145,
        ]
      }
    ],
  }
</script>

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!