更新时间:2023-01-17 19:41:40
您可以创建这样的递归函数来对汽车进行深度优先遍历
object。
You can create a recursive function like this to do a depth-first traversal of the cars
object.
var findObjectByLabel = function(obj, label) {
if(obj.label === label) { return obj; }
for(var i in obj) {
if(obj.hasOwnProperty(i)){
var foundLabel = findObjectByLabel(obj[i], label);
if(foundLabel) { return foundLabel; }
}
}
return null;
};
可以像这样调用
findObjectByLabel(car, "Chevrolet");