I think your approach is wrong. That's...to much code. I've made an object that seems to conform to the data you are looking through:
var data = [
{
username: "Alice",
userData : [
{
id: 1,
someData: "hello"
},
{
id: 2,
someData: "world"
}
]
},
{
username: "Bob",
userData : [
{
id: 3,
someData: "apple"
},
{
id: 4,
someData: "orange"
}
]
}
];
const findStuff = (data, lookup) => {
return data.find(
item => item.userData.some(
userDataItem => userDataItem.id === lookup.id
)
);
}
const shouldGetAlice = findStuff(data, {id: 1});
const shouldBeAliceAgain = findStuff(data, {id: 2});
const shouldGetBob = findStuff(data, {id: 3});
const shouldBeAnotherBob = findStuff(data, {id: 4});
console.log(shouldGetAlice.username);
console.log(shouldBeAliceAgain.username);
console.log(shouldGetBob.username);
console.log(shouldBeAnotherBob.username);
Start off with .find()
because you want to get a single item.
Inside the callback, you use .some()
to check the userData
properties, instead of looping through them yourself.
And that's it. You don't need to manually loop through each array.
If the descriptive variable names are removed, the function can be shortened to
const findStuff = (data, x) => {
return data.find(u => u.userData.some(ud => ud.id === x.id));
}
It can also be written on one line but it seems less clear.