NEXT_OFFSET条数组记录
NEXT_OFFSET records of array
我尝试获取元素数组并使用以下代码获取数组:
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
}));
结果看起来像这样:
{
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
}
]
}
但我试图获取元素之外的 NEXT_OFFSET,并且仅获取数组的最后一条记录。
它必须看起来像这个例子:
{
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
}
我怎样才能做到,就像示例中那样?
您可以使用 Array.reduce
来累积您的结果并在您点击最后一项时添加额外的字段。如果您将额外 属性 的初始值设置为 null
,它也会处理您的 'no results' 案例。
简化版是这样的:
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);
对于您的示例,如果您要批量获取 REQUEST_COUNT
项(例如 2000),它可能看起来像这样:
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 }
);
我尝试获取元素数组并使用以下代码获取数组:
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
}));
结果看起来像这样:
{
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
}
]
}
但我试图获取元素之外的 NEXT_OFFSET,并且仅获取数组的最后一条记录。
它必须看起来像这个例子:
{
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
}
我怎样才能做到,就像示例中那样?
您可以使用 Array.reduce
来累积您的结果并在您点击最后一项时添加额外的字段。如果您将额外 属性 的初始值设置为 null
,它也会处理您的 'no results' 案例。
简化版是这样的:
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);
对于您的示例,如果您要批量获取 REQUEST_COUNT
项(例如 2000),它可能看起来像这样:
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 }
);