我正在尝试在json中搜索属性名称,如果可以找到该属性,则返回它的值,否则返回null。
function returnValueAtProperty(string, exampleObject) {
for (ch1 in exampleObject) {
if (ch1 === string) {
return exampleObject[ch1];
} else if (typeof(exampleObject[ch1]) == 'object') {
var ob = exampleObject[ch1]
for (ch2 in ob) {
return returnValueAtProperty(string, ob[ch2])
}
} else if (Array.isArray(exampleObject[ch1])) {
var arr = exampleObject[ch1];
for (var i = 0; i < arr.length;) {
return returnValueAtProperty(string, arr[i])
}
}
}
}
var exampleObject = {
"key0": 42,
"key1": {
"key2": [{
"key3": "tag",
"key4": false
},
{
"key5": null,
"key6": [3, 141]
}
],
"key7": null
}
}
console.log(returnValueAtProperty("key3", exampleObject));
该函数应返回“标签”字符串。或者,如果我调用returnValueAtProperty(“ key6”,exampleObject)之类的函数,则应返回数组
答案 0 :(得分:0)
我只是更改了一点代码,我相信如果您在对象验证之前更改数组验证会更好,在javascript中,即使数组类型也可以返回对象,因此数组可能会进入对象 在下面的其他情况下,如果我向您展示我所做的代码更改
for(ch1 in exampleObject){
if(typeof ch1 === string){
return exampleObject[ch1];
} else if(Array.isArray(exampleObject[ch1])){
var arr = exampleObject[ch1];
for(var i=0; i<arr.length;){
return returnValueAtProperty(string,arr[i])
}
}
else if(typeof(exampleObject[ch1])=='object'){
var ob = exampleObject[ch1]
for(ch2 in ob){
return returnValueAtProperty(string, ob[ch2])
}
}
}
答案 1 :(得分:0)
只要不是null
,就可以执行一个循环并返回任何值。
function getValue(object, key) {
var k, temp;
if (key in object) return object[key]; // if found return value
for (k in object) { // iterate keys
if (object[k] && typeof object[k] === 'object') { // check not null and object
temp = getValue(object[k], key); // get sub value, if exists
if (temp !== null) return temp; // if not null return value
}
}
return null;
}
var object = { key0: 42, key1: { key2: [{ key3: "tag", key4: false }, { key5: null, key6: [3, 141] }], key7: null } };
console.log(getValue(object, "key3"));
console.log(getValue(object, "key6"));
console.log(getValue(object, "key7"));
console.log(getValue(object, "foo"));
将undefined
作为不存在的值,这是一种更好的方法。
function getValue(object, key) {
var k, temp;
if (key in object) return object[key];
for (k in object) {
if (object[k] && typeof object[k] === 'object') {
temp = getValue(object[k], key);
if (temp !== undefined) return temp;
}
}
}
var object = { key0: 42, key1: { key2: [{ key3: "tag", key4: false }, { key5: null, key6: [3, 141] }], key7: null } };
console.log(getValue(object, "key3"));
console.log(getValue(object, "key6"));
console.log(getValue(object, "key7"));
console.log(getValue(object, "foo"));
答案 2 :(得分:0)
function returnValueAtProperty(string,obj){
for(var i in obj){
if(i === string)
return obj[i]
else if(obj[i].constructor === Object) {
const val = returnValueAtProperty(string, obj[i]);
if(val) return val;
}
}
return null;
}
答案 3 :(得分:0)
您在这里犯了一些小错误。
检查:
function returnValueAtProperty(string, exampleObject) {
for (ch1 in exampleObject) {
if (ch1 === string) {
return exampleObject[ch1];
} else if (typeof(exampleObject[ch1]) == 'object') {
var ob = exampleObject[ch1];
return returnValueAtProperty(string, ob);
} else if (Array.isArray(exampleObject[ch1])) {
var arr = exampleObject[ch1];
for (var i = 0; i < arr.length;++i) {
return returnValueAtProperty(string, arr[i])
}
}
}
}
var exampleObject = {
"key0": 42,
"key1": {
"key2": [{
"key3": "tag",
"key4": false
},
{
"key5": null,
"key6": [3, 141]
}
],
"key7": null
}
}
console.log(returnValueAtProperty("key3", exampleObject));