Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

203
Views
Get index of parent given a value of a child array of arrays

I'm trying to get the index of the parent given an element inside the children, for example, I have a list variable (2) [Array(1), Array(2)] with this structure:

[
    [
        {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 tried using includes and findIndex to verify if such element exist first and then get the parent index:

list.includes('5555555555', 0);

I expected to get true because I asked include to begin searching in index 0 of the list exactly where "5555555555" element is, but i got false instead.

Also tried with:

list.findIndex(x => x.value === '5555555555');

I expected 0 since element 5555555555 is in parent 0 index. But got -1 instead.

I also tried using flat() to get into the children and the use includes, but then I lose the 0 and 1 index of the original list.

Expected output:

foo(list,'7777777777'); should return 1 since that element is inside the second array of the list or foo(list,'8888888888'); should also return 1 since that element is also inside the second array of the list.

Can someone guide me on how to approach this issue?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Use Array.findIndex with condition

const data = [
[
    {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"},
],
];

const index = data.findIndex(item => item.findIndex(node => node.value === "5555555555") > -1);
console.log(index)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!