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
    You are pushing an object and searching for a string. Commented Jun 17, 2013 at 13:33
  • @MatteoTassinari OOK...so can I search for an object? Commented Jun 17, 2013 at 13:34
  • 1
    @user2363025 no, using an object literal there won't work either. You have to write a simple loop to search for your object by property value. Also that's not a JSON list; it's a JavaScript array. Commented Jun 17, 2013 at 13:35
  • @Pointy could you give me a basic skeletal framework of what you're describing or point me to a site that can have them Commented Jun 17, 2013 at 13:36

1 Answer 1

2

You have to create a search facility tailored to your needs, perhaps like this:

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

Then you can do this:

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

If a matching element can't be found, the return value is undefined.

edit — I don't know much (well, anything) about the WinJS APIs, but it looks like those "List" objects aren't just simple arrays. I think you may have to do something like this (no guarantees as I can't test this):

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];
}
Sign up to request clarification or add additional context in comments.

10 Comments

This may need to be extended so it can apply to entire-object matches. Right now, the OP's example seems to intend to compare all values for matching (of course, functionally, I think it should just be the ID, as you've done it, so I voted you up). Oh, and just from my personal preference: An alternative way of checking "if (list[i] != null)" is "if (i in list)"
Yeah I don't think list[i] != null would work at all...if anything, compare to undefined, or typeof x !== "undefined". I can't see a reason why list[i] would be null, unless there's something special about WinJS.Binding.List
@Pointy Sincere thanks for the help. I keep getting undefined
@Katana314 I tried using if (i in list) also but I'm still getting undefined
@ Ian I tried comparing it to undefined and typeof x !== "undefined" but I'm still getting undefined. any idea why?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.