在Javascript

时间:2016-01-27 20:51:51

标签: angularjs

所以我试图通过Javascript中的id从下面的列表中检索一个集合。我怎样才能做到这一点?我最近一直在寻找方法,但我无法做到。

   {'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>'}

2 个答案:

答案 0 :(得分:1)

使用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 :(得分:0)

Find object by id in an array of JavaScript objects

的重复

使用jQuery.grep

// The following is being added to a list of type Foo
Foo(
    varone = "stringvalue",
    vartwo = "stringvalue",
    varthree = true
)