Advertisement
Guest User

Untitled

a guest
Apr 19th, 2014
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. var array = [{'id':1, 'Color':'blue'},
  2. {'id':2, 'Color':'orange'},
  3. {'id':3, 'Color':'green'},
  4. {'id':4, 'Color':'blue'} ];
  5.  
  6. var myFilter = array.filter(function (item) {
  7. return item.Color === 'blue';
  8. });
  9.  
  10. var result = array.filter(function(item) {
  11. return item.Color === 'blue';
  12. }).map(function(item) {
  13. return item.id;
  14. });
  15.  
  16. console.log(result)
  17. # [1, 4]
  18.  
  19. var result = _.chain(array)
  20. .filter(function(item) {
  21. return item.Color === 'blue';
  22. })
  23. .pluck("id")
  24. .value();
  25.  
  26. console.log(result);
  27. # [1, 4]
  28.  
  29. var result = _.chain(array)
  30. .where({Color: 'blue'})
  31. .pluck("id")
  32. .value();
  33.  
  34. console.log(result)
  35. # [1, 4]
  36.  
  37. console.log(_.pluck(_.where(array, {Color: 'blue'}), "id"));
  38. # [1, 4]
  39.  
  40. var result = [];
  41. for (var i = 0; i < array.length; i += 1) {
  42. if (array[i].Color === "blue") {
  43. result.push(array[i].id);
  44. }
  45. }
  46. console.log(result);
  47. # [1, 4]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement