[英]How do I correctly find a value in an object from an array, by matching it to another array with the values
So I have an array of values which I need to find:所以我有一个我需要找到的值数组:
const values = [ 'v4', 'w1']
And there is the array of objects where I need to look into:还有我需要查看的对象数组:
const nodes = [
{
"node": {
"options": [
{
"name": "Key1",
"value": "v1"
},
{
"name": "Key2",
"value": "w1"
}
]
}
},
{
"node": {
"options": [
{
"name": "Key1",
"value": "v1"
},
{
"name": "Key2",
"value": "w2"
}
]
}
},
{
"node": {
"options": [
{
"name": "Key1",
"value": "v2"
},
{
"name": "Key2",
"value": "w1"
}
]
}
},
{
"node": {
"options": [
{
"name": "Key1",
"value": "v2"
},
{
"name": "Key2",
"value": "w2"
}
]
}
},
{
"node": {
"options": [
{
"name": "Key1",
"value": "v3"
},
{
"name": "Key2",
"value": "w1"
}
]
}
},
{
"node": {
"options": [
{
"name": "Key1",
"value": "v3"
},
{
"name": "Key2",
"value": "w2"
}
]
}
},
{
"node": {
"options": [
{
"name": "Key1",
"value": "v4"
},
{
"name": "Key2",
"value": "w1"
}
]
}
},
{
"node": {
"options": [
{
"name": "Key1",
"value": "v4"
},
{
"name": "Key2",
"value": "w2"
}
]
}
},
{
"node": {
"options": [
{
"name": "Key1",
"value": "v5"
},
{
"name": "Key2",
"value": "w1"
}
]
}
},
{
"node": {
"options": [
{
"name": "Key1",
"value": "v5"
},
{
"name": "Key2",
"value": "w2"
}
]
}
}
]
And I basically need to find which is the node that matches the values array (in this example, the result should be nodes[6]
我基本上需要找到与值数组匹配的节点(在这个例子中,结果应该是nodes[6]
{
"node": {
"options": [
{
"name": "Key1",
"value": "v4"
},
{
"name": "Key2",
"value": "w1"
}
]
}
},
values
array will always have the length of node.options
, but one of the values can be undefined
in which case the first occurrence of the defined value should be returned. values
数组将始终具有node.options
的长度,但其中一个值可以是undefined
的,在这种情况下,应返回第一次出现的已定义值。
eg values = [ undefined, 'w1']
-> nodes[0]
例如values = [ undefined, 'w1']
-> nodes[0]
I managed to do something like我设法做类似的事情
const result = nodes.find(
(node) =>
options[0].value === values[0] &&
options[1].value === values[1]
);
But his won't work if options has length different than 2.但如果选项的长度不同于 2,他将不起作用。
Some other things I tried: (but seem to no be working)我尝试过的其他一些事情:(但似乎没有用)
nodes.find((node) => node.options.find(item => {
values.map(opt => opt === item.value)
}))
nodes.find((node) => node.options.filter(item => {
return values.includes(item.value)
}))
To found node that have option in values array you can do something like要在值数组中找到具有选项的节点,您可以执行类似的操作
var found = nodes.filter(elem => {
return !elem.node.options.some(option =>
!values.some(value => option.value === value)
);
});
const values = [ 'v4', 'w1']; const nodes = [ { "node": { "options": [ { "name": "Key1", "value": "v1" }, { "name": "Key2", "value": "w1" } ] } }, { "node": { "options": [ { "name": "Key1", "value": "v1" }, { "name": "Key2", "value": "w2" } ] } }, { "node": { "options": [ { "name": "Key1", "value": "v2" }, { "name": "Key2", "value": "w1" } ] } }, { "node": { "options": [ { "name": "Key1", "value": "v2" }, { "name": "Key2", "value": "w2" } ] } }, { "node": { "options": [ { "name": "Key1", "value": "v3" }, { "name": "Key2", "value": "w1" } ] } }, { "node": { "options": [ { "name": "Key1", "value": "v3" }, { "name": "Key2", "value": "w2" } ] } }, { "node": { "options": [ { "name": "Key1", "value": "v4" }, { "name": "Key2", "value": "w1" } ] } }, { "node": { "options": [ { "name": "Key1", "value": "v4" }, { "name": "Key2", "value": "w2" } ] } }, { "node": { "options": [ { "name": "Key1", "value": "v5" }, { "name": "Key2", "value": "w1" } ] } }, { "node": { "options": [ { "name": "Key1", "value": "v5" }, { "name": "Key2", "value": "w2" } ] } } ]; var found = nodes.filter(elem => { return.elem.node.options.some(option =>.values;some(value => option;value === value) ). }); console.log(found);
A combination of find
, reduce
and spread
operators can be used to achieve the solution.可以使用find
、 reduce
和spread
运算符的组合来实现解决方案。
find
the matching element.遍历数组元素以find
匹配的元素。reduce
and spread
to convert each element's options
to an array of corresponding optionValues
.使用reduce
和spread
将每个元素的options
转换为对应的optionValues
数组。optionValues
match values
.如果optionValues
匹配values
,则当前元素是匹配项。 (Using JSON.stringify
is one of the many ways to compare two arrays). (使用JSON.stringify
是比较两个数组的众多方法之一)。Something like this:像这样的东西:
const nodes = [ { "node": { "options": [ { "name": "Key1", "value": "v1" }, { "name": "Key2", "value": "w1" } ] } }, { "node": { "options": [ { "name": "Key1", "value": "v1" }, { "name": "Key2", "value": "w2" } ] } }, { "node": { "options": [ { "name": "Key1", "value": "v2" }, { "name": "Key2", "value": "w1" } ] } }, { "node": { "options": [ { "name": "Key1", "value": "v2" }, { "name": "Key2", "value": "w2" } ] } }, { "node": { "options": [ { "name": "Key1", "value": "v3" }, { "name": "Key2", "value": "w1" } ] } }, { "node": { "options": [ { "name": "Key1", "value": "v3" }, { "name": "Key2", "value": "w2" } ] } }, { "node": { "options": [ { "name": "Key1", "value": "v4" }, { "name": "Key2", "value": "w1" } ] } }, { "node": { "options": [ { "name": "Key1", "value": "v4" }, { "name": "Key2", "value": "w2" } ] } }, { "node": { "options": [ { "name": "Key1", "value": "v5" }, { "name": "Key2", "value": "w1" } ] } }, { "node": { "options": [ { "name": "Key1", "value": "v5" }, { "name": "Key2", "value": "w2" } ] } } ]; const values = [ "v4", "w1" ]; const getOptionValues = options => { return options.reduce((vals, curOption) => { vals = [...vals, curOption.value]; return vals; }, []); } const matchingNode = nodes.find(ele => { const optionValues = getOptionValues(ele.node.options); return JSON.stringify(values) === JSON.stringify(optionValues); }); console.log(matchingNode);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.