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

178
Views
NEXT_OFFSET records of array

I trying get the array of elements and I getting array with this code:

 const result = payload.map(({QUALITY, TEMPERATURE, SENSOR_READING_DATETIME, SOURCE_COMPONENT_ID, SENSOR_NAME, NEXT_OFFSET})=> ({
        TELEMATICS: {
          QUALITY,
          TEMPERATURE
        },
      SOURCE_COMPONENT_ID,
      SENSOR_NAME,
      SENSOR_READING_DATETIME,
      NEXT_OFFSET
    }));

in result it's looking like this:

{
    data": [
            {
                "TELEMATICS": {
                    "QUALITY": 91.98,
                    "TEMPERATURE": 20.5
                },
                "id": 118,
                "SENSOR_READING_DATETIME": "2021-09-24T04:53:06.801Z",
                "SOURCE_COMPONENT_ID": 1,
                "SENSOR_NAME": "TD2",
                "NEXT_OFFSET": 119
            }
            ,
            {
                "TELEMATICS": {
                    "QUALITY": 91.98,
                    "TEMPERATURE": 20.5
                },
                "id": 119,
                "SENSOR_READING_DATETIME": "2021-09-24T05:53:09.774Z",
                "SOURCE_COMPONENT_ID": 1,
                "SENSOR_NAME": "TD2",
                "NEXT_OFFSET": 120
            }
           ]
    }

But i trying get the NEXT_OFFSET outside the elements, and only last record of the array.

It's must be look like this example:

{
    data": [
            {
                "TELEMATICS": {
                    "QUALITY": 91.98,
                    "TEMPERATURE": 20.5
                },
                "id": 118,
                "SENSOR_READING_DATETIME": "2021-09-24T04:53:06.801Z",
                "SOURCE_COMPONENT_ID": 1,
                "SENSOR_NAME": "TD2"
            }
            ,
            {
                "TELEMATICS": {
                    "QUALITY": 91.98,
                    "TEMPERATURE": 20.5
                },
                "id": 119,
                "SENSOR_READING_DATETIME": "2021-09-24T05:53:09.774Z",
                "SOURCE_COMPONENT_ID": 1,
                "SENSOR_NAME": "TD2"
            }
           ]
        "NEXT_OFFSET": 120
    }

How i can do make it, like in the example?

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

0

You could use Array.reduce to accumulate your results and add the extra field when you hit the last item. If you set the initial value of the extra property to be null it also takes care of your 'no results' case.

A simplified version would be like this:

const payload = [
  { a: 1, b: 2 },
  { a: 5, b: 6 },
  { a: 10, b: 11 }
];

const res = payload.reduce(
  (acc, { a, b }, index) =>
    index === payload.length - 1
      ? { ...acc, data: [...acc.data, a],b }
      : { ...acc, data: [...acc.data, a] },

  { data: [], b:null }
);

console.log(res);

For your example, if you are fetching batches of REQUEST_COUNT items (e.g. 2000) it could look something like this:

const res = payload.reduce(
  (acc, { NEXT_OFFSET, ...item }, index) => {
    const data = [...acc.data, item];
    if (index < payload.length - 1) {
      return { data };
    } else {
      const nextOffset =
        payload.length < REQUEST_COUNT
          ? { NEXT_OFFSET: null }
          : { NEXT_OFFSET 
       return {data, ...nextOffset}
    }
  },
  { data: [], NEXT_OFFSET: null }
);
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!