简体   繁体   English

如何在也具有数组值的Object中搜索值?

[英]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.

相关问题 如何在 Object 中搜索包含作为数组值的子对象的值 - How to search for a value in Object which contains sub objects with as array values 如何过滤包含其他详细信息的对象内部的数组 - How to filter an array which is inside an object which has other details as well 如何搜索浏览器窗口对象以查找哪个对象或变量具有搜索值? - How to search the browser window object looking which object or variable has the searched-for value? 将具有数组值的 Object 转换为 Object 的另一个数组 - Convert Object which has value an Array to another Array of Object 获取具有属性的数组中的对象,该属性是包含匹配值的数组 - Get an Object in an Array that has a property which is an Array that contains a matching value 如何将具有相同属性名称的对象数组转换为从给定数组中的值连接值的对象? JS - How to transform an array of objects with the same property name into the object in which value is concatenated from values in a given array? JS 删除具有键值的嵌套数组中的对象 - delete an object in a nested array which has key value 我正在将 object 转换为该数组,但无法获得由数组中的键和值组成的预期数组,我错在哪里? - I am converting a object to this array, but cant get the expected array which would consist of Keys as well as values in the array, where am I wrong? 如何搜索和替换对象文字数组中的值 - How to search and replace value in array of object literals 如何在具有 JSON 值的 Amazon Athena 上查询(搜索)sql? - how to query(search) of sql on Amazon Athena which has JSON value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM