Consider this array structure:
const testData = [
{
group: "Team1",
info: [
{
key: 123,
person: "Alice",
type: "Football"
},
{
key: 456,
person: "Bob",
type: "Tennis"
},]
},
{
group: "Team2",
info: [
{
key: 890,
person: "Cindy",
type: "Swimming"
},
{
key: 45611,
person: "David",
type: "Soccer"
},]
},
]
I have a function to select the info.key
If I select Alice
, it should return 123
keeping only the key
value in the output:
{group: Team1, key: 123, person: "Alice", type: "Football"}
Currently, my method looks like this:
const tempIndex = testData?.findIndex(({ info }) =>
info.findIndex(({ key }) => key === e) // e represents the `key` value
);
console.log(tempIndex);
However, I am unsure if this is the best approach for achieving the desired result.
Is there an ES6 or lodash solution suitable for this scenario?