0

So I'm trying to retrieve a collection from this list below by the id in Javascript. How can I do that? I've been searching lately to find a way but I couldn't.

   {'id':1,"firstTeam":"Barcelona","secondTeam":"Real       Madrid","Time":"14:00","commentator":"Unknown","championship":"UEFA","channel":"BEIN SPORT","iframe":'<iframe width="560" height="315" src="https://www.youtube.com/embed/pQRO_5dtqrk" frameborder="0" allowfullscreen></iframe>'}

   {'id':2,"firstTeam":"Barcelona","secondTeam":"Real Madrid","Time":"14:00","commentator":"Unknown","championship":"UEFA","channel":"BEIN SPORT","iframe":'<iframe width="560" height="315" src="https://www.youtube.com/embed/pQRO_5dtqrk" frameborder="0" allowfullscreen></iframe>'}
jcdyer
  • 18,616
  • 5
  • 42
  • 49
  • Show what you've tried. Is the above an array? – tymeJV Jan 27 '16 at 20:53
  • Yes it is an array : var IframesList=[ { the objects i've just pasted }, {second object}]. and every line has an id. – Houssem Balty Jan 27 '16 at 20:56
  • By "collection by id", you mean, you search for one or more id values and want the rows back? What have you been trying so far? Please post a little bit context: How would you like the extraction of the id's to look like? – thst Jan 27 '16 at 21:02

2 Answers2

1

Use Array.prototype.filter():

 var testArray = [{'id':1,"firstTeam":"Barcelona","secondTeam":"Real Madrid","Time":"14:00","commentator":"Unknown","championship":"UEFA","channel":"BEIN SPORT","iframe":'<iframe width="560" height="315" src="https://www.youtube.com/embed/pQRO_5dtqrk" frameborder="0" allowfullscreen></iframe>'},
{'id':2,"firstTeam":"Barcelona","secondTeam":"Real Madrid","Time":"14:00","commentator":"Unknown","championship":"UEFA","channel":"BEIN SPORT","iframe":''}];

var filtered = testArray.filter(filterFunction);

// Let's assume you want to filter by ID, stored in $scope variable.
$scope.filterById = 2;


function filterFunction(val) {
  return value.id == $scope.filterById;
}
Batuta
  • 1,684
  • 17
  • 48
  • 62
0

It is dup of Find object by id in an array of JavaScript objects

Using jQuery.grep:

$.grep(myArray, function(e){ return e.id == id; })[0]
Community
  • 1
  • 1
Artem
  • 823
  • 8
  • 14