Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Comercial
    • Calculadora

0

45
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?

7 months 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 }
);
7 months ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos