In Json file type it's showing all the elements but I want specific elements should be displayed in the nodeArrayElements.

Mycode showing like this:

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

But I want the output like this:

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

1 Answers

0
Sourav Dutta On

This is Mutating Approach

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);

This is Non-Mutating Approach

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);

Preferably use the 2nd one i.e the Non-mutating approach, that doesn't modify your original object.