I recently found an interesting code snippet from a different question that allowed me to identify an object. However, I am now faced with the challenge of determining the position of that object within the array. Let's take a look at the example below:
var arr = [{
Id: 1,
Categories: [{
Id: 1
},
{
Id: 2
},
]
},
{
Id: 2,
Categories: [{
Id: 100
},
{
Id: 200
},
]
}
]
To locate the object based on the Id of the Categories, you can utilize the following code snippet:
var matches = [];
var needle = 100; // value to search for
arr.forEach(function(e) {
matches = matches.concat(e.Categories.filter(function(c) {
return (c.Id === needle);
}));
});
What if you also need to determine the position of the object in the array? For instance, how do you know that the object with Id = 100 is the second object in the main array and the first object in the Categories array?
If anyone has insights or solutions, it would be greatly appreciated! Thank you.