split out object property and values from an array
本问题已经有最佳答案,请猛点这里访问。
从这个JSON数组
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | { "result": [ { "id":"1", "name":"John", "type":"B", "score":"passed" }, { "id":"2", "name":"Alice", "type":"A", "score":"failed" } ] } |
如何将一些字段拆分成这样
1 2 3 4 5 6 7 8 9 10 11 12 13 | { "result": [ { "id":"1", "type":"B", }, { "id":"2", "type":"A", } ] } |
我不想在我的案例中使用拼接,上面只是示例代码。
Try this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | var input = { "result": [ { "id":"1", "name":"John", "type":"B", "score":"passed" }, { "id":"2", "name":"Alice", "type":"A", "score":"failed" } ] }; var output = { result: input.result.map(function(item) { return { id: item.id, type: item.type }; }) } |
在iterate阵列和remove时代属性 P / < >
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | var json = [ {"name":"john", "age":"30", "gender":"male <div class="suo-content">[collapse title=""]<ul><li>简单而有效的解决方案,很好。</li></ul>[/collapse]</div><p><center>[wp_ad_camp_1]</center></p><hr> <p> 像这样的尝试 P / < > <p> [cc lang="javascript"] var json = { "result": [{ "id":"1", "name":"John", "type":"B", "score":"passed" }, { "id":"2", "name":"Alice", "type":"A", "score":"failed" } ] }; json.result.forEach(function(item) { delete item.name; delete item.score; }); console.log(json); |
P / < >