如何从 Javascript 对象中删除不需要的密钥?
How do i remove an un-needed Key from a Javascript object?
我是 javascript 的新手,我正在使用带有 GraphQL 的 SilverStripe,它为我提供了边和节点:
这是一个充满对象的数组:
Genre: [
{ ID: "1", Title: "English", StudySubject: { "edges": [ { "node": { "ID": "4", "Title": "English"}}]}},
{ ID: "2", Title: "Maths", StudySubject: { "edges": [ { "node": { "ID": "4", "Title": "Maths"}}]}},
]
我想知道是否可以从每个对象中删除不需要的边和节点,这样我前端的点符号就更少了?
我希望它看起来像
Genre: [
{ ID: "1", Title: "English", StudySubject: { "ID": "4", "Title": "English"}},
{ ID: "2", Title: "Maths", StudySubject: { "ID": "4", "Title": "Maths"}},
]
您输入的数据不准确。在 JSON 数据结构中,您不能在同一级别有重复的键。以下答案修改了您的数据结构。
const data = [
{
ID: 1,
Subject:
{
edge : [
{node: {ID : 1, Title: "English"}}
]
}
},
{
ID: 2,
Subject: {
edge: [
{node: {ID: 1, Title: "Maths"}}
]
}
}
];
const result = data.map(x => ({...x, Subject: x.Subject.edge[0].node}));
console.log(result);
我是 javascript 的新手,我正在使用带有 GraphQL 的 SilverStripe,它为我提供了边和节点:
这是一个充满对象的数组:
Genre: [
{ ID: "1", Title: "English", StudySubject: { "edges": [ { "node": { "ID": "4", "Title": "English"}}]}},
{ ID: "2", Title: "Maths", StudySubject: { "edges": [ { "node": { "ID": "4", "Title": "Maths"}}]}},
]
我想知道是否可以从每个对象中删除不需要的边和节点,这样我前端的点符号就更少了?
我希望它看起来像
Genre: [
{ ID: "1", Title: "English", StudySubject: { "ID": "4", "Title": "English"}},
{ ID: "2", Title: "Maths", StudySubject: { "ID": "4", "Title": "Maths"}},
]
您输入的数据不准确。在 JSON 数据结构中,您不能在同一级别有重复的键。以下答案修改了您的数据结构。
const data = [
{
ID: 1,
Subject:
{
edge : [
{node: {ID : 1, Title: "English"}}
]
}
},
{
ID: 2,
Subject: {
edge: [
{node: {ID: 1, Title: "Maths"}}
]
}
}
];
const result = data.map(x => ({...x, Subject: x.Subject.edge[0].node}));
console.log(result);