Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

182
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda