我有一个数组中的对象,但我不知道如何打印它们
I have objects in an array and I don't know how to print them
数组中有对象,不知道怎么输出
不使用functionalList[0][1][2]`,想打印id和content的所有值,但是不知道怎么办
打印hi
会出错。
[
{
functionalList: [
{
id: 129,
content: 123,
},
],
},
{
functionalList: [
{
id: 130,
content: 35,
},
],
},
{
functionalList: [
{
id: 131,
content: 32,
},
],
},
];
let hi = [
values.functionalList.map((value: any) => {
return {
id: value.id,
content: value.content,
};
}),
];
console.log(hi);
首先,您必须删除 "content": 35 和 "content": 32 之后的逗号,因为它们会产生错误。
我给你一个想法,把你的数组放在一个变量中,例如叫做 'data'
// Online Javascript Editor for free
// Write, Edit and Run your Javascript code using JS Online Compiler
var data = [
{
"functionalList": [
{
"id": 129,
"content": 123
}
]
},
{
"functionalList": [
{
"id": 130,
"content": 35
}
]
},
{
"functionalList": [
{
"id": 131,
"content": 32
}
]
}
]
data.forEach(function(id){
// Get the data of particular id. You can loop over the specific id to access it's properties
Object.keys(id).forEach(function(prop){
console.log(id[prop]);
});
});
根据您的需要进行调整
您没有指定错误是什么(您确实应该指定)但是您发布的代码存在几个明显的问题。
您需要确保您的数组已分配给一个变量,因为在您的原始代码中它只是一个裸数组表达式。
除非您正在编写 TypeScript,否则您需要从箭头函数声明中删除 :any
,因为这是 TypeScript 类型声明,但在标准 JavaScript 中存在语法错误。
您似乎错误地构建了地图。映射遍历数组的所有元素,所有元素都是 属性 为 functionalList
的对象,但是你在数组上调用了 属性 而不是你应该做的它在地图函数中。
即便如此,functionalList
sub-objects 本身也是包含一项的数组,因此您应该引用所需的索引。由于示例中只有一个索引,因此我假设这就是您想要的索引,因此您将 [0] 添加到末尾。
所以你的地图函数应该是:
values.map((value) => {
return {
id: value.functionalList[0].id,
content: value.functionalList[0].content,
}
})
综合起来就是这个结果
let values = [
{
"functionalList": [
{
"id": 129,
"content": 123
}
]
},
{
"functionalList": [
{
"id": 130,
"content": 35
}
]
},
{
"functionalList": [
{
"id": 131,
"content": 32
}
]
}
]
let hi = [
values.map((value) => {
return {
id: value.functionalList[0].id,
content: value.functionalList[0].content,
}
})
]
console.log(hi);
如果 functionalList 的值将包含一个数据。您可以使用以下方法:
const values = [
{
functionalList: [
{
id: 129,
content: 123,
},
],
},
{
functionalList: [
{
id: 130,
content: 35,
},
],
},
{
functionalList: [
{
id: 131,
content: 32,
},
],
},
];
let hi = values.map((value) => {
return {
id: value.functionalList[0].id,
content: value.functionalList[0].content,
}
});
// adjust to what you want to do
hi.map((value) => {
console.log(`Id: ${value.id}, Content: ${value.content}`);
});
数组中有对象,不知道怎么输出
不使用functionalList[0][1][2]`,想打印id和content的所有值,但是不知道怎么办
打印hi
会出错。
[
{
functionalList: [
{
id: 129,
content: 123,
},
],
},
{
functionalList: [
{
id: 130,
content: 35,
},
],
},
{
functionalList: [
{
id: 131,
content: 32,
},
],
},
];
let hi = [
values.functionalList.map((value: any) => {
return {
id: value.id,
content: value.content,
};
}),
];
console.log(hi);
首先,您必须删除 "content": 35 和 "content": 32 之后的逗号,因为它们会产生错误。
我给你一个想法,把你的数组放在一个变量中,例如叫做 'data'
// Online Javascript Editor for free
// Write, Edit and Run your Javascript code using JS Online Compiler
var data = [
{
"functionalList": [
{
"id": 129,
"content": 123
}
]
},
{
"functionalList": [
{
"id": 130,
"content": 35
}
]
},
{
"functionalList": [
{
"id": 131,
"content": 32
}
]
}
]
data.forEach(function(id){
// Get the data of particular id. You can loop over the specific id to access it's properties
Object.keys(id).forEach(function(prop){
console.log(id[prop]);
});
});
根据您的需要进行调整
您没有指定错误是什么(您确实应该指定)但是您发布的代码存在几个明显的问题。
您需要确保您的数组已分配给一个变量,因为在您的原始代码中它只是一个裸数组表达式。
除非您正在编写 TypeScript,否则您需要从箭头函数声明中删除
:any
,因为这是 TypeScript 类型声明,但在标准 JavaScript 中存在语法错误。您似乎错误地构建了地图。映射遍历数组的所有元素,所有元素都是 属性 为
functionalList
的对象,但是你在数组上调用了 属性 而不是你应该做的它在地图函数中。即便如此,
functionalList
sub-objects 本身也是包含一项的数组,因此您应该引用所需的索引。由于示例中只有一个索引,因此我假设这就是您想要的索引,因此您将 [0] 添加到末尾。
所以你的地图函数应该是:
values.map((value) => {
return {
id: value.functionalList[0].id,
content: value.functionalList[0].content,
}
})
综合起来就是这个结果
let values = [
{
"functionalList": [
{
"id": 129,
"content": 123
}
]
},
{
"functionalList": [
{
"id": 130,
"content": 35
}
]
},
{
"functionalList": [
{
"id": 131,
"content": 32
}
]
}
]
let hi = [
values.map((value) => {
return {
id: value.functionalList[0].id,
content: value.functionalList[0].content,
}
})
]
console.log(hi);
如果 functionalList 的值将包含一个数据。您可以使用以下方法:
const values = [
{
functionalList: [
{
id: 129,
content: 123,
},
],
},
{
functionalList: [
{
id: 130,
content: 35,
},
],
},
{
functionalList: [
{
id: 131,
content: 32,
},
],
},
];
let hi = values.map((value) => {
return {
id: value.functionalList[0].id,
content: value.functionalList[0].content,
}
});
// adjust to what you want to do
hi.map((value) => {
console.log(`Id: ${value.id}, Content: ${value.content}`);
});