0

I have an object that has another objects inside, and I need to filter the objects by a value inside the second object.

userone:{
  name:"user1"
  lastname:"uzer1"
  task:{
     attendance: "1"
     taskname: "whatever"
  }
}
usertwo:{
  name:"user2"
  lastname:"uzer2"
  task:{
     attendance: "0"
     taskname: "whatever2"
  }
}

I want to get all the users whose attendance is 1. I have gone only as far as filtering by name and lastname, which I can do like this Object.values(userslist).filter(e => e.firstname == 'user1'); But not sure how to get inside the second object and filter by that value.

Also, the task object does not exist in all the objects.

Philip Stratford
  • 4,513
  • 4
  • 45
  • 71
asdasd
  • 19
  • 3
  • 2
    `e.task.attendance == 1` – Phil Aug 03 '20 at 09:20
  • Firstly `firstname` isn't in the object. But unless i'm missing something, you just need `e.task.taskname` – freefaller Aug 03 '20 at 09:20
  • `Object.values(userslist).filter(e => e.task.attendace == '1');` – rksh1997 Aug 03 '20 at 09:22
  • Perhaps you would be better going by using `[{},{}]` or `{456:{}, 36:{}}` (user IDs) rather than `{userone:{}, usertwo:{}}` ... But yeah. `e` is your *user*. And PS, you're missing *comma*s all over the place. – Roko C. Buljan Aug 03 '20 at 09:23
  • I get an error saying `cannot read property attendance of undefined`. Some of the objects do not have a `task` object inside them. How can I account for that? – asdasd Aug 03 '20 at 09:23
  • See this page : "https://stackoverflow.com/questions/38375646/filtering-array-of-objects-with-arrays-based-on-nested-value" – Elham Dabiri Aug 03 '20 at 09:27

0 Answers0