skip to Main Content

I have the people data below. where in people data there is "dataInduk" and inside "dataInduk" there is "dataAnak". how can I find people id data if I have "idAnak". this is my code

const people = [
{
  name: 'James',
  id: 1,
  dataInduk: [
    {
      idInduk: '1a',
      dataAnak: [
        {
          idAnak: '1a1'
        },
        {
          idAnak: '1a2'
        }
      ]
    },
    {
      idInduk: '1b',
      dataAnak: [
        {
          idAnak: '1b1'
        },
        {
          idAnak: '1b2'
        }
      ]
    }
  ]
},
{
  name: 'John',
  id: 2,
  dataInduk: [
    {
      idInduk: '2a',
      dataAnak: [
        {
          idAnak: '2a1'
        },
        {
          idAnak: '2a2'
        }
      ]
    },
    {
      idInduk: '2b',
      dataAnak: [
        {
          idAnak: '2b1'
        },
        {
          idAnak: '2b2'
        }
      ]
    }
  ]
}

];

for example:
when i find from idAnak = ‘2b2’ then show id "2" from people

2

Answers


  1. You can map your people array with forEach to get the result you want

    const personIdMapper = (idAnak, people) => {
        let matchedId;
        people.forEach((person) => {
        person.dataInduk.forEach((dataInduk) => {
          dataInduk.dataAnak.forEach((dataAnak) => {
            if (dataAnak.idAnak === idAnak) {
              matchedId = person.id;
            }
          });
        });
      })
      console.log("I am input id", idAnak);
      console.log("I am matched id", matchedId);
      return matchedId;
    }
    
    const people = [
    {
      name: 'James',
      id: 1,
      dataInduk: [
        {
          idInduk: '1a',
          dataAnak: [
            {
              idAnak: '1a1'
            },
            {
              idAnak: '1a2'
            }
          ]
        },
        {
          idInduk: '1b',
          dataAnak: [
            {
              idAnak: '1b1'
            },
            {
              idAnak: '1b2'
            }
          ]
        }
      ]
    },
    {
      name: 'John',
      id: 2,
      dataInduk: [
        {
          idInduk: '2a',
          dataAnak: [
            {
              idAnak: '2a1'
            },
            {
              idAnak: '2a2'
            }
          ]
        },
        {
          idInduk: '2b',
          dataAnak: [
            {
              idAnak: '2b1'
            },
            {
              idAnak: '2b2'
            }
          ]
        }
      ]
    }]
    
    const personIdMapper = (idAnak, people) => {
        let matchedId;
        people.forEach((person) => {
        person.dataInduk.forEach((dataInduk) => {
          dataInduk.dataAnak.forEach((dataAnak) => {
            if (dataAnak.idAnak === idAnak) {
              matchedId = person.id;
            }
          });
        });
      })
      console.log("I am input id", idAnak);
      console.log("I am matched id", matchedId);
      return matchedId;
    }
    
    personIdMapper('1a2', people);
    personIdMapper('2a1', people);
    personIdMapper('1b2', people);
    Login or Signup to reply.
  2. One liner:

    const people=[{name:"James",id:1,dataInduk:[{idInduk:"1a",dataAnak:[{idAnak:"1a1"},{idAnak:"1a2"}]},{idInduk:"1b",dataAnak:[{idAnak:"1b1"},{idAnak:"1b2"}]}]},{name:"John",id:2,dataInduk:[{idInduk:"2a",dataAnak:[{idAnak:"2a1"},{idAnak:"2a2"}]},{idInduk:"2b",dataAnak:[{idAnak:"2b1"},{idAnak:"2b2"}]}]}];
    
    console.log(
       people.filter(e => 
          e.dataInduk.some(j => j.dataAnak.some(k => k.idAnak === '2b2')))
             ?.map(i => i.id)
    );
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search