I am facing an issue while trying to access and manipulate an object within an array of objects using JavaScript in Google Apps Script. The error message I receive is: "TypeError: Cannot read property '0' of undefined"
The JSON data (obtained from an API response) is stored in the variable named "data" and looks like this:
[{
"values": {
"article_number": [
{
"locale": null,
"scope": null,
"data": "000000000010137290"
}
],
"article_description": [
{
"locale": null,
"scope": null,
"data": "SDS Mini Desktop Organiser 3 Draw Sil"
}
],
"marketing_description": [
{
"locale": "en_US",
"scope": null,
"data": "SDS Mini Desktop Organiser and 3 Drawers Mesh Silver"
}
]
}
}]
My goal is to extract the "data" element within each object and store them in a new object. Here is my current approach:
var products = [];
data.forEach(function(elem, i) {
products.push({
article_number: elem["article_number"][0]["data"],
article_description: elem["article_description"][0]["data"],
marketing_description: elem["marketing_description"][0]["data"]
});
});
I would greatly appreciate any help or guidance on how to resolve this issue. I have attempted removing the [0] index, but that only results in another error: "TypeError: Cannot read property 'data' of undefined". I am puzzled as I believed accessing index 0 of the array should work.