JS中给对象添加字段
<script>//将objA中className字段添加到person字段每一项中var objA = {classes: [{className: "移应202班",person: [{name: '小明',sex: "男"}, {name: '小明',.
·
利用for循环给数组对象添加字段
<script>
//将objA中className字段添加到person字段每一项中
var objA = {
classes: [{
className: "移应202班",
person: [{
name: '小明',
sex: "男"
}, {
name: '小明',
sex: "男"
}, {
name: '小明',
sex: "男"
}]
}, {
className: "移应201班",
person: [{
name: '小红',
sex: "女"
}, {
name: '小红',
sex: "女"
}, {
name: '小红',
sex: "女"
}]
}]
}
for (let i = 0; i < objA.classes.length; i++) {
for (let j = 0; j < objA.classes[i].person.length; j++) {
objA.classes[i].person[j]['className'] = objA.classes[i].className
}
}
console.log(objA)
</script>
更多推荐
已为社区贡献1条内容
所有评论(0)