gpt4 book ai didi

javascript - 将对象转换为 JSON 并删除键

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:29:31 28 4
gpt4 key购买 nike

我在尝试从现有对象创建新数组时遇到了一些困难。如果我执行以下操作

console.log(JSON.stringify(this.data))

输出是这样的

{
"nodes":[
{"id":1,"node":"0","name":"pizza","created_at":"2019-09-01 09:56:01","updated_at":"2019-09-01 09:56:01"},
{"id":2,"node":"1","name":"pasta","created_at":"2019-09-01 09:56:01","updated_at":"2019-09-01 09:56:01"},
{"id":3,"node":"2","name":"pie","created_at":"2019-09-01 09:56:01","updated_at":"2019-09-01 09:56:01"}
],
"links":[
{"id":1,"source":"0","target":"1","value":"451","created_at":"2019-09-01 09:56:01","updated_at":"2019-09-01 09:56:01"},
{"id":2,"source":"1","target":"3","value":"237","created_at":"2019-09-01 09:56:01","updated_at":"2019-09-01 09:56:01"}
]
}

我想做的是从节点中删除所有不是节点或名称的东西。从链接中,删除除源、目标和值之外的所有内容。我一直在尝试以下类似的事情但没有成功

const valuesToRemove = ['id', 'created_at', 'updated_at'];
this.data.links = this.data.links.filter((i) => (valuesToRemove.indexOf(i) === -1));

那么我该如何将上面的内容变成这样的东西呢?

{
"nodes":[
{"node":"0","name":"pizza"},
{"node":"1","name":"pasta"},
{"node":"2","name":"pie"}
],
"links":[
{"source":"0","target":"1","value":"451"},
{"source":"1","target":"3","value":"237"}
]
}

谢谢

最佳答案

您可以借助 Array.prototype.map 来映射数组属性并只在对象中保留所需的键:

const data = {
"nodes":[
{"id":1,"node":"0","name":"pizza","created_at":"2019-09-01 09:56:01","updated_at":"2019-09-01 09:56:01"},
{"id":2,"node":"1","name":"pasta","created_at":"2019-09-01 09:56:01","updated_at":"2019-09-01 09:56:01"},
{"id":3,"node":"2","name":"pie","created_at":"2019-09-01 09:56:01","updated_at":"2019-09-01 09:56:01"}
],
"links":[
{"id":1,"source":"0","target":"1","value":"451","created_at":"2019-09-01 09:56:01","updated_at":"2019-09-01 09:56:01"},
{"id":2,"source":"1","target":"3","value":"237","created_at":"2019-09-01 09:56:01","updated_at":"2019-09-01 09:56:01"}
]
};
function transform(data){
data.nodes = data.nodes.map(({node, name}) => ({node, name}));
data.links = data.links.map(({source, target, value}) => ({source, target, value}));
return data;
}
console.log(transform(data));

一种更动态的方法,而不是对要保留的 Prop 进行硬编码,而是将其作为 ES6 Set 提供(以便更快地查找)并将节点映射到正确的形式:

const data = {
"nodes":[
{"id":1,"node":"0","name":"pizza","created_at":"2019-09-01 09:56:01","updated_at":"2019-09-01 09:56:01"},
{"id":2,"node":"1","name":"pasta","created_at":"2019-09-01 09:56:01","updated_at":"2019-09-01 09:56:01"},
{"id":3,"node":"2","name":"pie","created_at":"2019-09-01 09:56:01","updated_at":"2019-09-01 09:56:01"}
],
"links":[
{"id":1,"source":"0","target":"1","value":"451","created_at":"2019-09-01 09:56:01","updated_at":"2019-09-01 09:56:01"},
{"id":2,"source":"1","target":"3","value":"237","created_at":"2019-09-01 09:56:01","updated_at":"2019-09-01 09:56:01"}
]
};

function transformDynamic(data, nodesToKeep, linksToKeep){
data.nodes = data.nodes.map((ele) => {
return Object.keys(ele).reduce((acc,key) => {
if(nodesToKeep.has(key)){
acc[key] = ele[key];
}
return acc;
}, {});
})
data.links =data.links.map((ele) => {
return Object.keys(ele).reduce((acc,key) => {
if(linksToKeep.has(key)){
acc[key] = ele[key];
}
return acc;
}, {});
});
return data;
}
const nodesToKeep = new Set(["node", "name"]);
const linksToKeep = new Set(["ource", "target", "value"]);
console.log(transformDynamic(data, nodesToKeep, linksToKeep));

关于javascript - 将对象转换为 JSON 并删除键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57749084/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com