我有一个对象数组,每个元素内都有一个对象,看起来像这样
[
{
"person": {
"name": "John",
"isActive": true,
"id": 1
}
},
{
"person": {
"name": "Ted",
"isActive": true,
"id": 2
}
}
]
我想将其转换为没有类名的这种格式:
[
{
"Name": "John",
"IsActive": true,
"id": 1
},
{
"Name": "Ted",
"IsActive": true,
"id": 2
}
]
结果使我可以将信息解析为Kendo UI网格。转换是在Typescript文件中完成的。
答案 0 :(得分:2)
您可以映射解构后的属性person
。
var data = [{ person: { name: "John", isActive: true, id: 1 } }, { person: { name: "Ted", isActive: true, id: 2 } }],
result = data.map(({ person }) => person);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 1 :(得分:1)
您可以使用.map
方法
const arr = [{"person": {"name": "John","isActive": true,"id": 1}},{"person": {"name": "Ted","isActive": true,"id": 2}}]
.map(({ person }) => {
return {
"Name": person.name,
"IsActive": person.isActive,
"id": person.id
}
})
console.log(arr)
答案 2 :(得分:1)
非常简单 people.map(p => p.person)
var people=[
{
"person": {
"name": "John",
"isActive": true,
"id": 1
}
},
{
"person": {
"name": "Ted",
"isActive": true,
"id": 2
}
}
]
var newPeople=people.map(p=>p.person)
console.log(newPeople)
答案 3 :(得分:1)
使用map
并返回具有所需键的对象
let data = [{
"person": {
"name": "John",
"isActive": true,
"id": 1
}
},
{
"person": {
"name": "Ted",
"isActive": true,
"id": 2
}
}
];
let newData = data.map(function(item) {
return {
Name: item.person.name,
IsActive: item.person.isActive,
id: item.person.id
};
});
console.log(newData)