数据结构定义为数组对象,每个对象里面又包含了对象,怎么获取子对象里面的某个属性?
async getAllData () { const { data: res } = await this.$http.get('http://127.0.0.1:4523/m1/2452239-0-default/api/tabledata')
console.log(res)
这是获取到的数组数据
(4) [{…}, {…}, {…}, {…}]0: age: {content: '81', show: true}
city: {content: '新疆维吾尔自治区 喀什地区', show: false}
name: {content: '陈伟', show: false}
tel: {content: '18183477152', show: true}
[[Prototype]]: Object
1: {name: {…}, age: {…}, city: {…}, tel: {…}}
2: {name: {…}, age: {…}, city: {…}, tel: {…}}
3: {name: {…}, age: {…}, city: {…}, tel: {…}}
length: 4
[[Prototype]]: Array(0)
想要获取子对象里面的每个对象里面的属性内容,打印出 undefined
async getAllData () { const { data: res } = await this.$http.get('http://127.0.0.1:4523/m1/2452239-0-default/api/tabledata')
console.log(res)
this.tableData = res.map(item => {
return console.log(Object.keys(item).map(key => {
return res[key]
}))
})
(4) [undefined, undefined, undefined, undefined]0: undefined
1: undefined
2: undefined
3: undefined
length: 4
[[Prototype]]: Array(0)
回答:
async getAllData () { const { data: res } = await this.$http.get('http://127.0.0.1:4523/m1/2452239-0-default/api/tabledata')
console.log(res)
this.tableData = res.map(item => {
return Object.keys(item).map(key => {
return item[key].content; // 获取子对象的 content 属性
})
})
console.log(this.tableData);
}
获取:
return key === 'age' ? item[key].content : null;
回答:
的你的题目代码中返回return log是什么鬼
随便一数组对象举个例子:
const array = [ { id: 1, name: 'a', detail: { age: 18, address: '111' } },
{ id: 2, name: 'b', detail: { age: 20, address: '222' } },
{ id: 3, name: 'c', detail: { age: 22, address: '333' } },
];
const ages = array.map((item) => item.detail.age);
console.log(ages); // 输出:[18, 20, 22]
以上是 数据结构定义为数组对象,每个对象里面又包含了对象,怎么获取子对象里面的某个属性? 的全部内容, 来源链接: utcz.com/p/934405.html