Advertisement
Guest User

Filtering array of objects

a guest
Jan 12th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. const people = [
  2. { "id": 1, "first_name": "Sunshine", "last_name": "Vesty", "email": "svesty0@latimes.com", "gender": "Female", "ip_address": "183.111.102.72", "vehicles": "Scion" },
  3. { "id": 2, "first_name": "Allyce", "last_name": "Heatley", "email": "aheatley1@ustream.tv", "gender": "Female", "ip_address": "250.195.26.202", "vehicles": "Toyota" },
  4. { "id": 3, "first_name": "Tanitansy", "last_name": "Bazley", "email": "tbazley2@canalblog.com", "gender": "Female", "ip_address": "208.37.216.91", "vehicles": "Mercury" },
  5. { "id": 4, "first_name": "Gustavo", "last_name": "Macy", "email": "gmacy3@hp.com", "gender": "Male", "ip_address": "43.110.63.96", "vehicles": "Ford" },
  6. { "id": 5, "first_name": "Arley", "last_name": "Allom", "email": "aallom4@pen.io", "gender": "Male", "ip_address": "168.59.248.11", "vehicles": "Mazda" },
  7. { "id": 6, "first_name": "Connie", "last_name": "Espada", "email": "cespada5@opensource.org", "gender": "Male", "ip_address": "43.12.37.107", "vehicles": "Lexus" },
  8. { "id": 7, "first_name": "Philippine", "last_name": "Voak", "email": "pvoak6@taobao.com", "gender": "Female", "ip_address": "144.91.4.160", "vehicles": "GMC" },
  9. { "id": 8, "first_name": "Jojo", "last_name": "Albasini", "email": "jalbasini7@bluehost.com", "gender": "Female", "ip_address": "255.44.68.78", "vehicles": "Ford" },
  10. { "id": 9, "first_name": "Francesco", "last_name": "Thorlby", "email": "fthorlby8@ucoz.ru", "gender": "Male", "ip_address": "14.45.191.100", "vehicles": "Ford" },
  11. { "id": 10, "first_name": "Weider", "last_name": "Bensley", "email": "wbensley9@live.com", "gender": "Male", "ip_address": "133.50.241.33", "vehicles": "Lotus" }
  12. ];
  13.  
  14. // filter array of objects with desired property
  15. const filteredPeople = Object.keys(people).filter((key) => people[key]['vehicles'] === 'Ford');
  16.  
  17. // map returns a new array, forEach does not
  18. const peopleWithFord = filteredPeople.map(x => people[x]);
  19.  
  20. console.log(peopleWithFord);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement