0

I have the following list constructed:

var names_Array= []; 
var names_List= new WinJS.Binding.List(names_Array);
names_List.push({ name: "Joe Dowling", image: "image/Joe Dowling.png", ClientID: "1234" }, { name: "Esteban Flamenco ", image: "image/Esteban Flamenco.png", ClientID: "6666" });

I want to be able to get the index of the list where the ID is 6666. My attempt thus far was to do the following:

var number = names_List.indexOf('{ name: "Esteban Flamenco ", image: "image/Esteban Flamenco.png", ClientID: "6666" }');
console.log(number);

But I am getting -1 (i.e. not found). Where am I going wrong?

4

1 回答 1

2

您必须创建一个适合您需求的搜索工具,可能像这样:

function findObject( list, property, value ) {
  var i;
  for (i = 0; i < list.length; ++i)
    if (list[i] != null && list[i][property] == value)
      return elem;
}

然后你可以这样做:

var client6666 = findObject(names_List, 'ClientID', '6666');

如果找不到匹配的元素,则返回值为undefined

编辑——我对 WinJS API 知之甚少(嗯,任何事情),但看起来那些“列表”对象不仅仅是简单的数组。我认为您可能必须做这样的事情(不能保证,因为我无法对此进行测试):

function findObject( list, property, value ) {
  var i, elem;
  for (i = 0; i < list.length; ++i)
    elem = list.getAt(i);
    if (elem != null && elem[property] == value)
      return list[i];
}
于 2013-06-17T13:42:34.947 回答