1. #1
    Sencha User steffenk's Avatar
    Join Date
    Jul 2007
    Location
    Haan, Germany
    Posts
    2,664
    Vote Rating
    7
    steffenk has a spectacular aura about steffenk has a spectacular aura about steffenk has a spectacular aura about

      0  

    Default Finding index in jsonData by field value

    Finding index in jsonData by field value


    Hi,

    maybe it is an easy JS-function, but i don't know how

    I have an Array like this (in view.jsonData):
    PHP Code:
    [{"uid":"20","name":"User 1"},{"uid":"18","name":"User 2"},{"uid":"11","name":"User 3"}] 
    Now i have the uid (e.g.11)
    How can i get the index of the array (view.jsonData[2] in this case) ?
    vg Steffen
    --------------------------------------
    Release Manager of TYPO3 4.5

  2. #2
    Sencha - Ext JS Dev Team evant's Avatar
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    17,000
    Vote Rating
    650
    evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute

      0  

    Default


    Code:
    var arr = [{"uid":"20","name":"User 1"},{"uid":"18","name":"User 2"},{"uid":"11","name":"User 3"}];
    
    for (int i = 0; i < arr.length; i++)
    {
       if (arr[i].uid == whatever)
          return i;
    }
    return -1;

  3. #3
    Sencha User steffenk's Avatar
    Join Date
    Jul 2007
    Location
    Haan, Germany
    Posts
    2,664
    Vote Rating
    7
    steffenk has a spectacular aura about steffenk has a spectacular aura about steffenk has a spectacular aura about

      0  

    Default


    yes, thx evant - i hoped that there is a built-in-routine like array.findByValue
    after writing the post i found the same way by iteration.
    vg Steffen
    --------------------------------------
    Release Manager of TYPO3 4.5

  4. #4
    Sencha - Ext JS Dev Team evant's Avatar
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    17,000
    Vote Rating
    650
    evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute

      0  

    Default


    You could do it, try:

    Code:
    function findByValue(arr, prop, val)
    {
       //do sanity checks on array and prop here.
       for (int i = 0; i < arr.length; i++)
       {
          if (arr[i][prop] === val)
             return i;
       }
       return -1;
    }
    var arr = [{"uid":"20","name":"User 1"},{"uid":"18","name":"User 2"},{"uid":"11","name":"User 3"}];
    alert(findByValue(arr, "uid", "11"));

Thread Participants: 1