I am trying to map through an array that contains null/empty elements, which outputs the following file:
{
"data": [
[null, { "value": "Position1" }, { "value": "Timmy" }],
[null, { "value": "Position2" }, { "value": "Bobby" }],
[null, { "value": "Position3" }, { "value": "johnny" }],
[null, { "value": "Position4" }, { "value": "Jimmy" }]
],
"positionCells": [
{ "row": 0, "column": 2, "position": "Position1" },
{ "row": 1, "column": 2, "position": "Position2" },
{ "row": 2, "column": 2, "position": "Position3" },
{ "row": 3, "column": 2, "position": "Position4" }
]
}
Instead of writing 'null' for the null element I would like to output something like: {"value": ""}
I tried doing that with the following, but when I do the console.log, it seems like map is skipping the null element. console.log does not show the null object. What am I missing?
let output = {
data: sheet.map((e) => {
return e.map((f) => {
console.log('f: ', f);
if (f === null || f === undefined) {
return { value: '' };
} else {
return f;
}
});
}),
positionCells: positionCells,
};
console.log('output: ', output);
I am trying to map through an array that contains null/empty elements, which outputs the following file:
{
"data": [
[null, { "value": "Position1" }, { "value": "Timmy" }],
[null, { "value": "Position2" }, { "value": "Bobby" }],
[null, { "value": "Position3" }, { "value": "johnny" }],
[null, { "value": "Position4" }, { "value": "Jimmy" }]
],
"positionCells": [
{ "row": 0, "column": 2, "position": "Position1" },
{ "row": 1, "column": 2, "position": "Position2" },
{ "row": 2, "column": 2, "position": "Position3" },
{ "row": 3, "column": 2, "position": "Position4" }
]
}
Instead of writing 'null' for the null element I would like to output something like: {"value": ""}
I tried doing that with the following, but when I do the console.log, it seems like map is skipping the null element. console.log does not show the null object. What am I missing?
let output = {
data: sheet.map((e) => {
return e.map((f) => {
console.log('f: ', f);
if (f === null || f === undefined) {
return { value: '' };
} else {
return f;
}
});
}),
positionCells: positionCells,
};
console.log('output: ', output);
Share
edited Aug 27, 2021 at 18:25
romellem
6,5132 gold badges35 silver badges69 bronze badges
asked Aug 27, 2021 at 18:15
codecustardcodecustard
3213 silver badges9 bronze badges
3
- I have copied the data and code into my own IDE (running js file through node.js), I could not seem to recreate the error, It worked exactly to your specification. How are you running this? – Joshua Smart Commented Aug 27, 2021 at 18:35
-
1
Please update your post to show this code even running, because right now
sheet
is not defined and it's definitely not the data you're showing, because that's an object, not an array. Ideally: reduce your code because you can trivially prove to yourself thatnull
maps just fine, even something simple like taking thatdata
property and then runningdata.map(list => list.map(e => e===null))
shows no problems there. – Mike 'Pomax' Kamermans Commented Aug 27, 2021 at 18:39 - It might have to do with the array element being empty and not null. – codecustard Commented Aug 27, 2021 at 19:14
3 Answers
Reset to default 2Thank you for asking this question, I see what you did here. This might fix your issue here:
data: sheet.map(e => e.map(f => f?f:{value: ''))
const sheet = [
[null, { "value": "Position1" }, { "value": "Timmy" }],
[null, { "value": "Position2" }, { "value": "Bobby" }],
[null, { "value": "Position3" }, { "value": "johnny" }],
[null, { "value": "Position4" }, { "value": "Jimmy" }]
];
const positionCells = [
{ "row": 0, "column": 2, "position": "Position1" },
{ "row": 1, "column": 2, "position": "Position2" },
{ "row": 2, "column": 2, "position": "Position3" },
{ "row": 3, "column": 2, "position": "Position4" }
];
let output = {
data: sheet.map(e => e.map(f => f? f: { value: '' })),
positionCells: positionCells,
};
console.log('output: ', output);
Just have the nested map
run on the elements and the return the non-null items and null items separately. Have a look at this snippet
const input = {
data: [
[null, { value: "Position1" }, { value: "Timmy" }],
[null, { value: "Position2" }, { value: "Bobby" }],
[null, { value: "Position3" }, { value: "johnny" }],
[null, { value: "Position4" }, { value: "Jimmy" }]
],
positionCells: [
{ row: 0, column: 2, position: "Position1" },
{ row: 1, column: 2, position: "Position2" },
{ row: 2, column: 2, position: "Position3" },
{ row: 3, column: 2, position: "Position4" }
]
};
const data = input.data.map((value) => {
const nested = value.map((item) => {
if (!item) {
return { value: "" };
} else {
return item;
}
});
return nested;
});
const output = { ...input, data };
console.log(output);
Thank you for asking this question, I see what you did here. This might fix your issue here:
data: sheet.map(e => {
e.forEach((f, i) => {
if(f === null || f === undefined) {
e[i] = {value: ''};
}
})
return e;
})
const sheet = [
[null, { "value": "Position1" }, { "value": "Timmy" }],
[null, { "value": "Position2" }, { "value": "Bobby" }],
[null, { "value": "Position3" }, { "value": "johnny" }],
[null, { "value": "Position4" }, { "value": "Jimmy" }]
];
const positionCells = [
{ "row": 0, "column": 2, "position": "Position1" },
{ "row": 1, "column": 2, "position": "Position2" },
{ "row": 2, "column": 2, "position": "Position3" },
{ "row": 3, "column": 2, "position": "Position4" }
];
let output = {
data: sheet.map(e => {
e.forEach((f, i) => {
if(f === null || f === undefined) {
e[i] = {value: ''};
}
})
return e;
}),
positionCells: positionCells,
};
console.log('output: ', output);