GarrethTrent
GarrethTrent

Reputation: 35

Accessing object inside array in Javascript is returning TypeError: Cannot read property 'data' of undefined error

I'm trying to access an object inside of an array of objects using javascript in Google Apps Script, but it's giving me an error - TypeError: Cannot read property '0' of undefined

The JSON (received from an API result) is as follows, and is stored in the "data" variable:

[{
    "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"
            }
        ]
    }
}]

I'm trying to access the "data" element and add each data element to a new object:

var product = [];
  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 really appreciate the assistance in solving this. I've tried removing the [0], but this just returns the error "TypeError: Cannot read property 'data' of undefined". I'm at my whits end, as I assumed that calling index 0 of the array would work.

Upvotes: 0

Views: 357

Answers (2)

Dr. Selva Mary G
Dr. Selva Mary G

Reputation: 688

let data1 = [{
    "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"
            }
        ]
    }
}]
var product = []; 
data1.forEach(function (elem, i) { 
product.push({ 
article_number: elem.values["article_number"][0]["data"], 
article_description: elem.values['article_description'][0].data, 
marketing_description: elem.values['marketing_description'][0].data });
}); 
console.log(product) 

let data1 = [{
    "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"
            }
        ]
    }
}]
var product = [];
data1.forEach(function (elem, i) {
    product.push({ article_number: elem.values.article_number[0].data, article_description: elem.values.article_description[0].data, marketing_description: elem.values.marketing_description[0].data });
});
console.log(product)

Upvotes: 0

lejlun
lejlun

Reputation: 4419

This is a bit more verbose than what you were going for, but here's what I did.

  1. Iterate through the keys of the object.
  2. Find the data property of the matching object.
  3. Create a new object with pairs in the format of key: data.

Output:

[
  {
    "article_number": "000000000010137290",
    "article_description": "SDS Mini Desktop Organiser 3 Draw Sil",
    "marketing_description": "SDS Mini Desktop Organiser and 3 Drawers Mesh Silver"
  }
]

Full code below:

let recieved = [{
  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",
    }, ],
  },
}, ];

let allValues = [];

for (let i = 0; i < recieved.length; i++) {

  let newObj = Object.keys(recieved[i].values).reduce((acc, curr) => {
    acc[curr] = recieved[i].values[curr][0].data;
    return acc;
  }, {});

  allValues.push(newObj);
}

console.log(allValues);

Upvotes: 2

Related Questions