我希望标题能正确表达我要解决的问题。我需要做的是从检查数组中搜索匹配元素的对象,然后返回该匹配对象的索引。去:
const checkArray = ['18A38', '182B92', '85F33']; // these are the values to match
const dataOject = [
0 => ['id'=>'853K83', 'isGO'=>false], // this is the object to search through
1 => ['id'=>'85F33', 'isGO'=>true],
2 => ['id'=>'97T223', 'isGO'=>true],
3 => ['id'=>'18A38', 'isGO'=>false],
4 => ['id'=>'182B92', 'isGO'=>true],
...
];
what i need to do is find the matching index so i can then check if the isGO
flag is set. this is what i was trying when i dead-ended:
results = checkArray.forEach(function(value, index){
if (dataObject.findIndex(function(k=> k == value))) results.push(k);
// i know 'results.push(k)' is not right, but it's the essence of what i want. :P
};
what i am expecting is that results
will be an array of indexes that i can then go back and check the dataObject
for set isGO
flags; results
should look like this:
results = [3, 1, 4];
but i'm stumped on how to make the findIndex
complete properly. i've read this and this and this but, while educational, they aren't dealing with an array and an object. i do have underscore in this project, but, again, haven't found anything that i comprehend as useful in this scenario.
如何使它以给我所需的方式运行?
除了返回索引外,返回对象本身是否更容易?
That will return all objects having
id
found in yourcheckArray
.Having these objects in
matchedObjects
, you can iterate through them and do whatever you wish.