javascript - Get specific elements from arrays within array in jQuery -


i having trouble getting want out of jquery. have ajax call returns , array of arrays.

my jquery looks this...

$.each(search_results, function() {   $.each(this, function(results)   {     var first_name = '';     var last_name = '';     $.each(this, function(index, value)     {       if(index == 3 )       {         last_name = value;       }        if(index == 5)       {         first_name = value;         var contact_name = first_name + " " + last_name;         var result_item = '<li class="list-group-item">' + contact_name + '</li>';           $(id).append(result_item);       }     });   }); }); 

this works , , good, i'm returning hundreds of records. looping through each element in array pickout 2 5 elements seems way work.

is there way this...

  $.each(search_results, function() {   $.each(this, function(results)   {     $.each(this, function(result)     {        var first_name = result[5];        var last_name = result[3];         var contact_name = first_name + " " + last_name;         var result_item = '<li class="list-group-item">' + contact_name + '</li>';           $(id).append(result_item);       }     });   }); }); 

i found similar question here, answer seems odd me. there has way....

i hope helps more

array / json structure:

object { search_results: […] } search_results : array [ […], […], […], … ] [0..99] 0 : array [ "all", "stuff", "i want", … ] 

from understood question, have array similar to:

 var search_results = [                          [                             [1.0, 1.1, 1.2, 1.3, 1.4, 1.5],                              [2.0, 2.1, 2.2, 2.3, 2.4, 2.5]                          ]                       ]; 

and want access elements 1.3, 1.5, 2.3, 2.5....

following code access elements.

$.each(search_results, function(l1_index, l1_result) {    $.each(this, function(l2_index, l2_result)    {      if(l2_result.length > 5)      {          var last_name = l2_result[3];          var first_name = l2_result[5];          var contact_name = first_name + " " + last_name;          var result_item = '<li class="list-group-item">' + contact_name + '</li>';          $(id).append(result_item);      }   }); }); 

p.s: if can post array structure can better understand question.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -