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

185
Views
Get specific values from object when mapping through nested object

I want to return an array with specific values of _time & _value. The function that I made is as followed:

getMachineData(data) {
    console.log(data)
    const result = data.map((innerArray) => {
        console.log(typeof innerArray)
        console.log(innerArray)
        // Map over the inner array
        return innerArray.map((item) => {
            console.log(item)
            return ({
                time: item._time,
                value: item._value
            });
        });
    });
    console.log(result);
};

But in this code the last console.log is never hit.

The innerArray data is as followed:

{ "result": "_result", "table": 0, "_start": "2021-01-06T14:35:53Z", "_stop": "2022-01-06T14:35:53Z", "_time": "2021-10-11T10:58:30Z", "_value": 0, "_field": "approved", "_measurement": "oee-data", "end_time_str": "2021-10-11T10:59:00Z", "machine": "almo", "start_time_str": "2021-10-11T10:58:30Z" }

The output:

enter image description here

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

0

Your innerArray is not not an Array but an Object. You can not map over an Object like innerArray.map... which will probably throw an error.

about 4 years ago · Juan Pablo Isaza Report

0

innerArray is an Object, so dont have to iterate. Just access the value directly. Replace below code

return innerArray.map((item) => {
            console.log(item)
            return ({
                time: item._time,
                value: item._value
            });
        });

with

return {
         time: innerArray._time,
         value: innerArray._value
       };

The optimized code will look like

getMachineData(data) {
    const result = data.map(({_time, _value}) => ({time: _time, value:_value }));
    console.log(result);
};
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!