在 Javascript 中访问数组内的对象返回 TypeError: 无法读取未定义错误的属性“数据”
2021-07-30
320
我尝试使用 Google Apps Script 中的 javascript 访问对象数组内的对象,但出现错误 - TypeError:无法读取未定义的属性“0”
JSON(从 API 结果接收)如下,存储在“data”变量中:
[{
"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"
}
]
}
}]
我尝试访问“data”元素并将每个数据元素添加到新对象:
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"]
});
});
我非常感谢帮助解决这个问题。我尝试删除 [0],但这只会返回错误“TypeError:无法读取未定义的属性‘data’”。我束手无策,因为我认为调用数组的索引 0 会起作用。
2个回答
这比你想要的要冗长一些,但这就是我所做的。
- 遍历对象的键。
-
查找匹配对象的
data
属性。 -
创建一个新的对象,其中包含
key: data
格式的对。
输出:
[
{
"article_number": "000000000010137290",
"article_description": "SDS Mini Desktop Organiser 3 Draw Sil",
"marketing_description": "SDS Mini Desktop Organiser and 3 Drawers Mesh Silver"
}
]
完整代码如下:
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);
lejlun
2021-07-30
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)
Dr. Selva Mary G
2021-07-30