I have an object array to which I'm add a new property (theNewFieldToAdd) using map, however I only want to add the property if the index is of a certain number.
Any ideas?
rows.map((row, index) => ({
...row,
theNewFieldToAdd: true
})),
You can use for example the short-circuit evaluation to make it a bit more concise:
let rows = [{ key: "a" }, { key: "b" }, { key: "c" }];
let result = rows.map((row, index) => ({
...row,
...(index === 0 && { theNewFieldToAdd: true }),
}));
console.log(result);