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
    16,834
    Vote Rating
    609
    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
    16,834
    Vote Rating
    609
    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

Turkiyenin en sevilen filmlerinin yer aldigi xnxx internet sitemiz olan ve porn sex tarzi bir site olan mobil porno izle sitemiz gercekten dillere destan bir durumda herkesin sevdigi bir site olarak tarihe gececege benziyor. Sitenin en belirgin ozelliklerinden birisi de Turkiyede gercekten kaliteli ve muntazam, duzenli porno izle siteleri olmamasidir. Bu yuzden iste. Ayrica en net goruntu kalitesine sahip adresinde yayinlanmaktadir. Mesela diğer sitelerimizden bahsedecek olursak, en iyi hd porno video arşivine sahip bir siteyiz. "The Best anal porn videos and slut anus, big asses movies set..." hd porno faketaxi