鉴于以下三个对象,什么是有效的方法来返回包含键值对的第一个对象?
var obj = {
item1: {
name: 'apple',
color: 'red'
},
item2: {
name: 'blueberry',
color: 'blue'
},
item3: {
name: 'cherry',
color: 'red'
}
};
var obj2 = {
collection: [
{
item1: {
name: 'apple',
color: 'red'
},
item2: {
name: 'blueberry',
color: 'blue'
},
item3: {
name: 'cherry',
color: 'red'
}
}
]
};
var obj3 = {
items: [
{
item1: {
name: 'apple',
color: 'red'
}
},
{
item2: {
name: 'blueberry',
color: 'blue'
},
},
{
item3: {
name: 'cherry',
color: 'red'
}
}
]
};
对于以下三个语句,我希望获得相同的结果:
getObject(obj, 'color', 'red');
getObject(obj2, 'color', 'red');
getObject(obj3, 'color', 'red');
输出:
{
name: 'apple',
color: 'red'
}
到目前为止,这是我所拥有的,但是我认为它缺少某个地方的闭包,因为在函数调用自身时它会中断:
function getObject(arg, key, val) {
if (typeof arg!=='object') return null;
switch (Object.prototype.toString.call(arg)) {
case '[object Array]':
for (var i=0; i<arg.length; ++i) {
getObject(arg[i], key, val);
}
break;
case '[object Object]':
for (var i in arg) {
if (arg.hasOwnProperty(i)) {
if (typeof arg[i]==='object') {
getObject(arg[i], key, val);
} else {
if (i===key && arg[i]===val) {
return arg;
}
}
}
}
break;
}
}
答案 0 :(得分:4)
您可以使用此功能,该功能还将搜索更深层嵌套的数据结构。
function getObject(obj, prop, value) {
if (Object(obj) !== obj) return; // It is not an object
if (obj[prop] === value) return obj; // Found it
for (var key in obj) {
var result = getObject(obj[key], prop, value);
if (result) return result; // Found it
}
}
var obj = {item1: {name: 'apple',color: 'red'},item2: {name: 'blueberry',color: 'blue'},item3: {name: 'cherry',color: 'red'}};
var obj2 = {collection: [{item1: {name: 'apple',color: 'red'},item2: {name: 'blueberry',color: 'blue'},item3: {name: 'cherry',color: 'red'}}]};
var obj3 = {items: [{item1: {name: 'apple',color: 'red'}},{item2: {name: 'blueberry',color: 'blue'},},{item3: {name: 'cherry',color: 'red'}}]};
console.log(getObject(obj, 'color', 'red'));
console.log(getObject(obj2, 'color', 'red'));
console.log(getObject(obj3, 'color', 'red'));
答案 1 :(得分:2)
如果找到了对象,则可以使用Array#some
作为快捷方式。
function getObject(object, key, value) {
var result;
if (!object || typeof object !== 'object') return;
if (object[key] === value) return object;
Object.values(object).some(v => result = getObject(v, key, value));
return result;
}
var obj = { item1: { name: 'apple', color: 'red' }, item2: { name: 'blueberry', color: 'blue' }, item3: { name: 'cherry', color: 'red' } },
obj2 = { collection: [{ item1: { name: 'apple', color: 'red' }, item2: { name: 'blueberry', color: 'blue' }, item3: { name: 'cherry', color: 'red' } }] },
obj3 = { items: [{ item1: { name: 'apple', color: 'red' } }, { item2: { name: 'blueberry', color: 'blue' } }, { item3: { name: 'cherry', color: 'red' } }] };
console.log(getObject(obj, 'color', 'red'));
console.log(getObject(obj2, 'color', 'red'));
console.log(getObject(obj3, 'color', 'red'));
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 2 :(得分:0)
您在getObject
中的方法是可以的,但是,作为一般方法,您应该对所有数组进行变换。您可以使用函数map
来为查找过程准备阵列。
函数find
根据特定的谓词“找到”对象。
这是假定索引与键->
索引i==0
->
items1
等相关的。
var obj = {item1: {name: 'apple',color: 'red'},item2: {name: 'blueberry',color: 'blue'},item3: {name: 'cherry',color: 'red'}};
var obj2 = {collection: [{item1: {name: 'apple',color: 'red'},item2: {name: 'blueberry',color: 'blue'},item3: {name: 'cherry',color: 'red'}}]};
var obj3 = {items: [{item1: {name: 'apple',color: 'red'}},{item2: {name: 'blueberry',color: 'blue'},},{item3: {name: 'cherry',color: 'red'}}]};
let getObject = (o, key, value) => o.find(obj => obj[key] === value);
console.log(getObject(Object.values(obj), 'color', 'red'));
console.log(getObject(obj2.collection.map((o, i) => o[`item${i+1}`]), 'color', 'red'));
console.log(getObject(obj3.items.map((o, i) => o[`item${i+1}`]), 'color', 'red'));
.as-console-wrapper { min-height: 100%; }
答案 3 :(得分:0)
您可以递归调用该函数以到达没有任何对象成员的对象,并按键和值对其进行过滤:
function getObject(obj, k, v) {
for (var key in obj) {
if (typeof obj[key] === 'object') {
return getObject(obj[key], k, v);
} else if (key === k && obj[key] === v) {
return(obj);
}
}
}
var obj = {
item1: {
name: 'apple',
color: 'red'
},
item2: {
name: 'blueberry',
color: 'blue'
},
item3: {
name: 'cherry',
color: 'red'
}
};
var obj2 = {
collection: [
{
item1: {
name: 'apple',
color: 'red'
},
item2: {
name: 'blueberry',
color: 'blue'
},
item3: {
name: 'cherry',
color: 'red'
}
}
]
};
var obj3 = {
items: [
{
item1: {
name: 'apple',
color: 'red'
}
},
{
item2: {
name: 'blueberry',
color: 'blue'
},
},
{
item3: {
name: 'cherry',
color: 'red'
}
}
]
};
console.log(getObject(obj, 'color', 'red'));
console.log(getObject(obj2, 'color', 'red'));
console.log(getObject(obj3, 'color', 'red'));