Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ahmetonurslmz/a39ca6a186286732891ff5e49715011f to your computer and use it in GitHub Desktop.
Save ahmetonurslmz/a39ca6a186286732891ff5e49715011f to your computer and use it in GitHub Desktop.
Find nested array of objects inside an array of objects

Find nested array of objects inside an array of objects

To illustrate, We have a nested array object in object

const Clubs= new Schema({
  {cid: 1,
  c_name: "Fenerbahce",
  c_players: [
    {
    pid: 1,
    p_name: "Elif Elmas"
    },
    {
    pid: 2,
    p_name: "Victor Moses"
    }
     ]
    }
  ]}
})

to find specific object in nested array, we must use this structure;

/*We have player id variable:  incomingID*/
const { body : { incomingID } } = req;
const data = Clubs.cplayers.find(x => x.pid == incomingID);
console.log(data)

output

 {pid: 1,
  p_name: "Elif Elmas"
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment