Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

179
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda