有什么办法可以删除gojs中的nodeArrayelements

时间:2019-04-13 19:42:10

标签: javascript

在Json文件类型中,它显示了所有元素,但我希望特定元素应显示在nodeArrayElements中。

Mycode显示如下:

{
  "class": "GraphLinksModel",
  "nodeDataArray": [{ "text": "Entity", "figure": "Circle", "fill": "#00AD5F", "key": -1, "loc": "-190 -190" }],
  "linkDataArray": []
}

但是我想要这样的输出:

{
  "nodeDataArray"[{ "text": "Entity" }],
  "linkDataArray": []
}

1 个答案:

答案 0 :(得分:0)

这是变异方法

const data = {
  "class": "GraphLinksModel",
  "nodeDataArray": [{ "text": "Entity", "figure": "Circle", "fill": "#00AD5F", "key": -1, "loc": "-190 -190" }],
  "linkDataArray": []
};
data['nodeDataArray'].map(function(item) { 
    delete item.figure;
    delete item.fill;
    delete item.key;
    delete item.loc;
    return item; 
});
delete data['class'];
console.log(data);

这是非可变方法

const data = {
  "class": "GraphLinksModel",
  "nodeDataArray": [{ "text": "Entity", "figure": "Circle", "fill": "#00AD5F", "key": -1, "loc": "-190 -190" }],
  "linkDataArray": []
}
const newNodeDataArray = data['nodeDataArray'].map(({figure, fill, key, loc, ...keepAttrs}) => keepAttrs);
const newData = {'nodeDataArray': newNodeDataArray, 'linkDataArray': data.linkDataArray};
console.log(newData);

最好使用第二种方法,即不变异方法,该方法不会修改原始对象。