admin管理员组文章数量:1279042
I have a javascript object with multiple nested objects like this :
var stats = {
bookServed: {
redis: 90,
s3: 90,
signedUrl: 70
},
errors: {
redis: {
bookService: 70,
mapi: 50,
capi: 30
},
AWS: {
signedUrl: 70,
downloadBook: 50,
searchBook: 10
},
decryption: 60
}
};
What would be the cleanest way to iterate through all its properties and set each value to 0
for instance. I wrote something like this
for (var property in stats) {
if (stats.hasOwnProperty(property)) {
if (typeof property === "object") {
for (var sub_property in property)
if (property.hasOwnProperty(sub_property)) {
sub_property = 0
}
} else {
property = 0;
}
}
}
I'm willing to use a library like underscore.js to do the task properly.
I have a javascript object with multiple nested objects like this :
var stats = {
bookServed: {
redis: 90,
s3: 90,
signedUrl: 70
},
errors: {
redis: {
bookService: 70,
mapi: 50,
capi: 30
},
AWS: {
signedUrl: 70,
downloadBook: 50,
searchBook: 10
},
decryption: 60
}
};
What would be the cleanest way to iterate through all its properties and set each value to 0
for instance. I wrote something like this
for (var property in stats) {
if (stats.hasOwnProperty(property)) {
if (typeof property === "object") {
for (var sub_property in property)
if (property.hasOwnProperty(sub_property)) {
sub_property = 0
}
} else {
property = 0;
}
}
}
I'm willing to use a library like underscore.js to do the task properly.
Share Improve this question edited Oct 13, 2015 at 21:04 Kevin B 95.1k16 gold badges167 silver badges186 bronze badges asked Oct 13, 2015 at 20:54 AmaynutAmaynut 4,2717 gold badges44 silver badges46 bronze badges 5- Any library you use is going to use a similar method of recursive looping. I assume what you have works, and you're just looking for something... "cleaner" (whatever that means)? – Kevin B Commented Oct 13, 2015 at 20:58
- My solution works only for 2 levels of nested objects, in my example I have 3 level, that's why I want something recursive that go to the deepest level no matter how many levels I have in my object. – Amaynut Commented Oct 13, 2015 at 21:01
- so make a function that does the step, and if an object is found instead of a number, call the function again on the sub object. – Kevin B Commented Oct 13, 2015 at 21:02
- Ok I got the idea, but how to make it recursive? – Amaynut Commented Oct 13, 2015 at 21:05
- 2 "and if an object is found instead of a number, call the function again on the sub object." that would make it recursive. – Kevin B Commented Oct 13, 2015 at 21:06
3 Answers
Reset to default 7Relatively simple recursion problem, I would use a function that calls itself when sub objects are found. I would also avoid using a for in loop, and instead use a forEach on the object's keys (it's much faster, and doesn't require a hasOwnProperty check.)
function resetValuesToZero (obj) {
Object.keys(obj).forEach(function (key) {
if (typeof obj[key] === 'object') {
return resetValuesToZero(obj[key]);
}
obj[key] = 0;
});
}
var stats = {
bookServed: {
redis: 90,
s3: 90,
signedUrl: 70
},
errors: {
redis: {
bookService: 70,
mapi: 50,
capi: 30
},
AWS: {
signedUrl: 70,
downloadBook: 50,
searchBook: 10
},
decryption: 60
}
};
console.log(stats.errors.AWS.signedUrl); // 70
resetValuesToZero(stats);
console.log(stats.errors.AWS.signedUrl); // 0
The below solution uses object-scan, which uses an iterative implementation to traverse the input. Also note that this will traverse into any array structures similar to the accepted answer.
// const objectScan = require('object-scan');
const rewriter = objectScan(['**'], {
rtn: 'count',
filterFn: ({ value, parent, property }) => {
if (typeof value === 'number') {
parent[property] = 0;
return true;
}
return false;
}
});
const stats = { bookServed: { redis: 90, s3: 90, signedUrl: 70 }, errors: { redis: { bookService: 70, mapi: 50, capi: 30 }, AWS: { signedUrl: 70, downloadBook: 50, searchBook: 10 }, decryption: 60 } };
console.log(rewriter(stats)); // returns count of replaces
// => 10
console.log(stats);
/* =>
{ bookServed: { redis: 0, s3: 0, signedUrl: 0 },
errors:
{ redis: { bookService: 0, mapi: 0, capi: 0 },
AWS: { signedUrl: 0, downloadBook: 0, searchBook: 0 },
decryption: 0 } }
*/
.as-console-wrapper {max-height: 100% !important; top: 0}
<script src="https://bundle.run/[email protected]"></script>
Disclaimer: I'm the author of object-scan
function checkNPE(obj) {
for(const name in obj){console.log('name: ' + JSON.stringify(name, null, 4));
if (typeof obj[name] === 'object') {console.log('object name: ' + JSON.stringify(name, null, 4));
checkNPE(obj[name]);
} // if (typeof obj[name] === 'object') {
} // for(const name in obj){
}; // function checkNPE (obj) {
var stats = {
bookServed: {
redis: 90,
s3: 90,
signedUrl: 70
},
errors: {
redis: {
bookService: 70,
mapi: 50,
capi: 30
},
AWS: {
signedUrl: 70,
downloadBook: 'a',
searchBook: 10
},
decryption: 60
}
};
checkNPE(stats);
本文标签: javascriptIterate through an object which has nested objects in nodeJsStack Overflow
版权声明:本文标题:javascript - Iterate through an object which has nested objects in nodeJs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741274672a2369659.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论