本文介绍了角度:无法重新生成对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想修改并重新生成对象数组.请检查我创建的 stackblitz.com
I want to modify and re-generate an array of objects.Please check my created stackblitz.com
请查看数据转储:
data = [
{
"player settings": [
{
"id": 1,
"labelName": "site language",
"labelValue": [
{
"id": 1,
"languageName": "ARABIC",
"language": "لغتك",
"languageCode": "AE"
},
{
"id": 2,
"languageName": "CHINESE",
"language": "你的语言",
"languageCode": "ZH"
},
],
"dataType": "DD",
"selectedData": "2"
},
{
"id": 2,
"labelName": "subtitle language",
"labelValue": [
{
"id": 1,
"languageName": "ARABIC",
"language": "لغتك",
"languageCode": "AE"
},
{
"id": 2,
"languageName": "CHINESE",
"language": "你的语言",
"languageCode": "ZH"
},
],
"dataType": "DD",
"selectedData": "1"
},
]
},
{
"channel": [
{
"id": 11,
"labelName": "channel label",
"dataType": "TX",
"selectedData": "jhfh"
}
]
},
{
"others": [
{
"id": 16,
"labelName": "others label",
"dataType": "TX",
"selectedData": "dhgdhg"
}
]
}
];
如何在以下条件下修改并重新生成对象:
How can I modify and re-generate the object with the following conditions:
如果 dataType
==='DD',则将 selectedData
转换为数字.
if dataType
=== 'DD' then convert selectedData
into number.
我写了下面的代码,但卡在这里,但出现错误ERROR错误:this.data [key] .forEach不是函数
:
I wrote the below code but stuck here and got error ERRORError: this.data[key].forEach is not a function
:
for (let key in this.data) {
this.data[key].forEach(obj => {
if (obj.dataType === "DD") {
obj.selectedData = +(obj.selectedData || 0)
}
});
}
推荐答案
数组的每个元素都是一个对象.而且对象没有 forEach()
方法.您可以尝试以下功能
Each element of the array is an object. And object doesn't have a forEach()
method. You could try the following function
ngOnInit() {
this.data.forEach(item => {
for (const key in item) { // <-- `item.forEach()` won't work because it's an object
item[key].forEach(subItem => {
if (subItem.dataType === 'DD') {
subItem.selectedData = +(subItem.selectedData || 0);
}
})
}
});
console.log(this.data);
}
我已经修改了您的 Stackblitz
这篇关于角度:无法重新生成对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!