• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

135
Vistas
NEXT_OFFSET registros de matriz

Intento obtener la matriz de elementos y obtengo una matriz con este código:

 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 }));

en resultado se ve así:

 { 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 } ] }

Pero intento obtener NEXT_OFFSET fuera de los elementos, y solo el último registro de la matriz.

Debe verse como este ejemplo:

 { 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 }

¿Cómo puedo hacerlo, como en el ejemplo?

about 3 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Puede usar Array.reduce para acumular sus resultados y agregar el campo adicional cuando presione el último elemento. Si establece que el valor inicial de la propiedad adicional sea null , también se encarga de su caso de "sin resultados".

Una versión simplificada sería así:

 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);

Para su ejemplo, si está obteniendo lotes de REQUEST_COUNT artículos (por ejemplo, 2000), podría verse así:

 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 3 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 Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda