Javascript 如何取消选择嵌套JSON对象?

Javascript 如何取消选择嵌套JSON对象?,javascript,node.js,json,Javascript,Node.js,Json,目前我的代码是console.logbody:,body.readResults。但我不想要t和r的值。如何取消选择它们并获得其余的 "readResults": [ { "id": "Channel1.Device1.Coil", "s": true, "r": "", "v": true, "t": 1553586722927 } ] 你可以很容易地做到: function cleanMyResult(re

目前我的代码是console.logbody:,body.readResults。但我不想要t和r的值。如何取消选择它们并获得其余的

"readResults": [
   {
       "id": "Channel1.Device1.Coil",
       "s": true,
       "r": "",
       "v": true,
       "t": 1553586722927
    }
]
你可以很容易地做到:

function cleanMyResult(result) {
    const cleanResult = Object.assign({}, result); // this will preserve your original object, but it costs some performance
    delete cleanResult.t;
    delete cleanResult.r;
    return cleanResult;
}

const cleanedResults = body.readResults.map(cleanMyResult);
console.log(cleanedResults);
还请检查许多已经很好的答案,以便:


格式化您的代码,接受测试,然后重新格式化您的问题,使其易懂。将有什么内容取代t&R和@YaHtet good的可能重复项!所以请用复选标记接受我的答案,这样其他人就能很快理解你的问题已经解决了。