Así que estoy intentando recuperar una colección de esta lista a continuación por id en Javascript. ¿Cómo puedo hacer eso? He estado buscando últimamente para encontrar una manera pero no pude.

   {'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>'}
0
Houssem Balty 27 ene. 2016 a las 23:51

2 respuestas

La mejor respuesta

Utilice 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;
}
1
Batuta 27 ene. 2016 a las 20:57

Es dup de Buscar objeto por id en una matriz de Objetos JavaScript

Con jQuery.grep:

$.grep(myArray, function(e){ return e.id == id; })[0]
0
Community 23 may. 2017 a las 11:46