[英]How to search a value in Object which has values in array as well?
I've an object as below; 我有一个对象,如下所示;
FOO: {
BAR: [9,32,8,12 ],
ZET: 4,
BETA: [3,14,6,2],
ALPHA: 37
},
I need to search values of this object to match values with keys
. 我需要搜索该对象的值以将其值与
keys
匹配。 There are tons of samples which gives keys
with a singular value but I couldn't find any sample which also able to search in values those are in arrays. 有很多样本为
keys
提供了一个奇异的值,但是我找不到任何能够搜索数组中值的样本。
How can I be able to search whole of those values of FOO
object and match with keys
? 我如何能够搜索
FOO
对象的所有这些值并与keys
匹配?
Note : The aim; 注 :目的; I'll looking for an input and if given input is
2
then expect to get the key BETA
or if given input is 4
then ZET
. 我将寻找一个输入,如果给定的输入为
2
则期望得到密钥BETA
如果给定的输入为4
则期望ZET
。
So basically the values will be some pre-defined unique values already. 因此,基本上,这些值已经是一些预定义的唯一值。
You could get the keys, create an array of values and check with includes
. 您可以获取键,创建值数组并使用
includes
检查。 Return the found key. 返回找到的密钥。
function find(object, value) { return Object .keys(object) .find(k => [].concat(object[k]).includes(value)); } var object = { FOO: { BAR: [9, 32, 8, 12], ZET: 4, BETA: [3, 14, 6, 2], ALPHA: 37 } }; console.log(find(object.FOO, 4)); console.log(find(object.FOO, 8));
You can do it with a simple for...in
loop and .concAT()
and .includes()
methods of arrays: 您可以使用简单的
for...in
循环以及.concAT()
和.includes()
方法来实现:
let obj = { BAR: [9,32,8,12 ], ZET: 4, BETA: [3,14,6,2], ALPHA: 37 }; let locator = (o, v) => { for (var prop in o) { if([].concat(o[prop]).includes(v)){ return prop; } } } console.log(locator(obj, 2)); console.log(locator(obj, 4));
Something like this should work: 这样的事情应该起作用:
// What we're searching FOO = { BAR: [9,32,8,12 ], ZET: 4, BETA: [3,14,6,2], ALPHA: 37 }; function findValue(findValue, obj) { return Object.entries(FOO) .filter(([key,value]) => value === findValue || Array.isArray(value) && value.includes(findValue)) .map(([key,value])=> key); } function testfindValue(value, obj) { console.log("testfindValue: \\nInput: " + value, "Result: ", findValue(value,obj)); } testfindValue(4, FOO); testfindValue(6, FOO); testfindValue(32, FOO); testfindValue(99, FOO);
You can simply loop though the object and search for the value to identify the key. 您可以简单地遍历对象并搜索值以标识键。 If multiple matches are found, an array of keys that matches the result will be returned.
如果找到多个匹配项,则将返回与结果匹配的键数组。
function objSearch(key, obj) { const keys = []; for (let item in obj) { if (obj.hasOwnProperty(item)) { if (obj[item] === key || (Array.isArray(obj[item]) && obj[item].indexOf(key) > -1)) { keys.push(item); } } } return keys; } const obj = { FOO: { BAR: [9, 32, 8, 12], ZET: 4, BETA: [3, 14, 6, 2], ALPHA: 37 } }; const res1 = objSearch(14, obj.FOO); // Exist const res2 = objSearch(15, obj.FOO); // Does not exist const res3 = objSearch(37, obj.FOO); // Exist console.log(res1); console.log(res2); console.log(res3);
Try this snippet, 试试这个片段
I am checking conditions for object values whether its array or value and applied condition as per that. 我正在检查对象值的条件,无论是数组还是值以及所应用的条件。 I kept parent key dynamic too.
我也使父键保持动态。
var abc = { FOO: { BAR: [9, 32, 8, 12], ZET: 4, BETA: [3, 14, 6, 2], ALPHA: 37 }, DAB: { DBAR: [9, 32, 8, 12], DZET: 4, DBETA: [3, 14, 6, 2], DALPHA: 37 }, }; function retTitle(abc, parent, k) { var title = ''; $.each(abc[parent], function(x, y) { if ((Array.isArray(y) && y.indexOf(k) != -1) || (!Array.isArray(y) && y == k)) { title = x; return; } }); return title; } var title = retTitle(abc, 'DAB', 4); console.log(title);
You can use a simple for...in
loop to iterate over keys. 您可以使用简单的
for...in
循环来迭代键。 Then check if the value is a number and equals to what you look for. 然后检查该值是否为数字并等于您要查找的值。 If it's not a number then it's an array - so you check that this array includes the number you are looking for.
如果不是数字,则为数组-因此,请检查该数组是否包含要查找的数字。
var FOO = { BAR: [9,32,8,12 ], ZET: 4, BETA: [3,14,6,2], ALPHA: 37 }; function findKeyByValue(obj, val) { for (var i in FOO) { if (!obj.hasOwnProperty(i)) continue; if (typeof FOO[i] === 'number') { if(FOO[i] === val) return i; } else { if(FOO[i].includes(val)) return i; } } } console.log(findKeyByValue(FOO, 4));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.