Is it possible to retrieve the index of the parent based on a specific element within the children? For instance, consider the following structure stored in a variable called list
:
[
[
{id: "1", contactType: {id: "phoneNumber", company: {id: "01", name: "Company01"}}, value: "5555555555"},
],
[
{id: "2", contactType: {id: "phoneNumber", company: {id: "03", name: "Company03"}}, value: "7777777777"},
{id: "3", contactType: {id: "phoneNumber", company: {id: "05", name: "Company05"}}, value: "8888888888"},
],
]
I attempted to use the includes
and findIndex
methods to determine if a specific element exists and then capture the index of its parent:
list.includes('5555555555', 0);
My expectation was to receive true
as I instructed includes
to search from index 0 where the element "5555555555"
is located. Unfortunately, the result was false
.
Another approach involved:
list.findIndex(x => x.value === '5555555555');
The anticipated outcome was 0
since the element 5555555555
resides within the first array. Nevertheless, the result returned was -1
.
An alternative attempt using flat()
to access the children and subsequently applying includes
led to the loss of reference to the original indexes 0
and 1
in the initial list
.
Desired result:
foo(list,'7777777777');
should yield 1
as this element is situated within the second array of the list
. Similarly, foo(list,'8888888888');
should also produce 1
since the element is contained within the same array.
Any guidance on resolving this issue would be greatly appreciated.