'How to insert null on the same line index of a different object?
I have 2 Objects with arrays
Edit*
imageSRC = (
https://store.com/image.png-2022,
https://store.com/image.png-2023,
https://store.com/image.png-2024,
https://store.com/image.png-2025,
https://store.com/image.png-2026,
https://store.com/image.png-2027,
.... (19 total)
ImageID = (
12345689,
45678915,
null,
34567890,
89123456,
89012345,
.... 22(total))
End Edit*
The imageSRC holds urls, and have a count of 19 items that are comma seperated The imageID holds image IDs, and have a count of 22 items that are comma seperated
The number of items for both arrays will vary based on different products.
These arrays represents variant images from my store of a product. Some of the imageIDs have null ("") values due to some variants not having images aka imageSRC.
The problem I am facing is that the imageSRC doesnt align with the imageID once the imageID hits a null value. The imageSRC and imageIDs are already in order by the variations index and cannot change.
So, the solution for me would be to insert a null value in the imageSRC object at the same exact index within the imageID object.
example
What is currently happening
https://store.com/image.png-2022 123456789
https://store.com/image.png-2023 145679879
https://store.com/image.png-2024 null
https://store.com/image.png-2025 891234567
What I want to happen
https://store.com/image.png-2022 123456789
https://store.com/image.png-2023 145679879
null null
https://store.com/image.png-2024 891234567
I do not want to get rid of the null values (using continue), as I am wanting to keep them as a "placeholder" for a different step in my setup. I should end up with the same length of indexes as the imageID when the code is executed. In this case 22.
I have tried everything from (push, continue, break, and etc.) and although I have came close several times. I'm still not able to get this right.
Here's the Javascript that I have been using ...
let imageSRC = inputData.imageSRC.split(",");
let imageID = inputData.imageID.split(",");
function toObject(imageSRC, imageID) {
let result = {};
for (let i = 0; i < imageID.length; i++) {
if (imageID[i] === "") { continue;
} result[imageSRC[i]] = imageID[i];
}
return result
}
output = toObject(imageSRC, imageID);`
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
