forEach | const sensorMapping = [];
const unmappedSensors = [];
sensors?.forEach((sensor) => {
if (sensor.loc && sensor.ID) {
sensorMapping.push({
serialNumber: sensor.ID,
location: sensor.loc,
});
} else if (sensor.ID) {
unmappedSensors.push(sensor.ID);
}
});
| ready |
filter+map | const sensorMapping = sensors?.filter(sensor =>
sensor.loc && sensor.ID
).map(sensor => ({
serialNumber: sensor.ID,
location: sensor.loc,
})) || [];
const unmappedSensors = sensors?.filter(sensor => !sensor.loc && sensor.ID).map(sensor => sensor.ID) || [];
| ready |
for...of | const sensorMapping = [];
const unmappedSensors = [];
if (sensors) {
for (const sensor of sensors) {
if (sensor.loc && sensor.ID) {
sensorMapping.push({
serialNumber: sensor.ID,
location: sensor.loc,
});
} else if (sensor.ID) {
unmappedSensors.push(sensor.ID);
}
}
}
| ready |
reduce | const { sensorMapping, unmappedSensors } = (sensors || []).reduce(({ sensorMapping, unmappedSensors }, sensor) => {
if (sensor.loc && sensor.ID) {
sensorMapping.push({
serialNumber: sensor.ID,
location: sensor.loc,
});
} else if (sensor.ID) {
unmappedSensors.push(sensor.ID);
}
return { sensorMapping, unmappedSensors };
}, {
sensorMapping: [],
unmappedSensors: [],
})
| ready |
reduce without destructuring | const { sensorMapping, unmappedSensors } = (sensors || []).reduce((acc, sensor) => {
if (sensor.loc && sensor.ID) {
acc.sensorMapping.push({
serialNumber: sensor.ID,
location: sensor.loc,
});
} else if (sensor.ID) {
acc.unmappedSensors.push(sensor.ID);
}
return acc;
}, {
sensorMapping: [],
unmappedSensors: [],
})
| ready |
reduce without destructuring without else statement | const { sensorMapping, unmappedSensors } = (sensors || []).reduce((acc, sensor) => {
if (sensor.loc && sensor.ID) {
acc.sensorMapping.push({
serialNumber: sensor.ID,
location: sensor.loc,
});
return acc
}
if (sensor.ID) {
acc.unmappedSensors.push(sensor.ID);
}
return acc;
}, {
sensorMapping: [],
unmappedSensors: [],
})
| ready |