[英]ES5 JavaScript: Find specific property in nested objects and return its value
I need to return the value of a specific property of nested objects with ES5 syntax.我需要使用 ES5 语法返回嵌套对象的特定属性的值。 Every object can have its own structure, so the needed property can be on different levels/places.每个对象都可以有自己的结构,因此所需的属性可以位于不同的级别/位置。 Eg -> I have three different objects and the value of property "source" is needed:例如 -> 我有三个不同的对象,需要属性“source”的值:
So how can I return the value of the specific property with consideration, that it can be on any position in object?那么,如何考虑返回特定属性的值,它可以位于对象中的任何位置?
var first_data = {
area: "South",
code: "S265",
info: {
category: "enhanced",
label: "AB | 27AS53",
variable: "VR"
},
list: {
area: "Mid",
details: [
{
source: "Spain",
totals: 12,
products: [
{
name: "ABC",
brand: "Nobrand",
id: "111",
category: "Men",
}
]
}
]
},
time: 1654775446138
};
var second_data = {
area: "South",
code: "S265",
info: {
category: "enhanced",
label: "AB | 27AS53",
variable: "VR"
},
list: {
area: "Mid",
details: [
{
products: [
{
name: "ABC",
brand: "Nobrand",
id: "111",
category: "Men",
_new: {
source: "Spain",
totals: 12
}
}
]
}
]
},
time: 1654775446138
};
var third_data = {
area: "South",
code: "S265",
info: {
category: "enhanced",
label: "AB | 27AS53",
variable: "VR"
},
list: {
area: "Mid",
details: [
{
products: [
{
name: "ABC",
brand: "Nobrand",
id: "111",
category: "Men"
}
]
}
]
},
time: 1654775446138
};
I first tried to solve it with ES6, so that I can rewrite it in a second step into ES5.我首先尝试用 ES6 解决它,以便我可以在第二步将其重写为 ES5。 Here is what I have so far.这是我到目前为止所拥有的。 The first problem is that here I am getting a false, but the property exists.第一个问题是,我在这里得到一个错误,但该属性存在。
var propertyExists = function (obj, key) {
if(obj === null || obj === undefined) {
return false;
}
for(const k of Object.keys(obj)) {
if(k === key) {
return obj[k]
}
else {
const val = obj[k];
if(typeof val === 'object') {
if(propertyExists(val, key) === true) {
return true;
}
}
}
}
return false;
}
propertyExists(first_data, 'source')
Your propertyExists
function didn't work because it returned the value of source
but it later checked if the value is equal to true
(as described by Felix Kling in a comment above ).您的propertyExists
函数不起作用,因为它返回了source
的值,但它后来检查了该值是否等于true
(如 Felix Kling 在上面的评论中所述)。
Here's my implementation (originally in ES6 but I used a typescript compiler with target set to ES5):这是我的实现(最初在 ES6 中,但我使用了目标设置为 ES5 的打字稿编译器):
var findProp = function (obj, prop) {
if (typeof obj != "object") {
return false;
}
if (obj.hasOwnProperty(prop)) {
return obj[prop];
}
for (var _i = 0, _a = Object.keys(obj); _i < _a.length; _i++) {
var p = _a[_i];
if (typeof obj[p] === "object") {
var t = findProp(obj[p], prop);
if (t) {
return t;
}
}
}
return false;
};
Note: It might be faster to detect which object structure it is and retrieve the value because you would then know where it is.注意:检测它是哪个对象结构并检索值可能会更快,因为这样您就会知道它在哪里。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.