按照js按值传递res[obj.category].bytes = '修改'应该是不会修改__array里面的值才对,但是res.__array.push(res[obj.category] = obj);使用这种赋值方式,修改res[obj.category].bytes值__array里面的值会一起变更。这要理解成java的引用类型吗?var dataObject=[ { "category" : "Search Engines", "hits" : 5, "bytes" : 50189 }, { "category" : "Content Server", "hits" : 1, "bytes" : 17308 }, { "category" : "Content Server", "hits" : 1, "bytes" : 47412 }, { "category" : "Search Engines", "hits" : 1, "bytes" : 7601 }, { "category" : "Business", "hits" : 1, "bytes" : 2847 }, { "category" : "Content Server", "hits" : 1, "bytes" : 24210 }, { "category" : "Internet Services", "hits" : 1, "bytes" : 3690 }, { "category" : "Search Engines", "hits" : 6, "bytes" : 613036 }, { "category" : "Search Engines", "hits" : 1, "bytes" : 2858 } ];var result = dataObject.reduce(function(res, obj) { if (!(obj.category in res)) res.__array.push(res[obj.category] = obj); else res[obj.category].bytes = '修改' return res;}, {__array:[]})
添加回答
举报
0/150
提交
取消