0

I am having some trouble with getting what I want out of jQuery. I have an ajax call that returns and array of arrays.

my jQuery looks something like 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 and is all well and good, but I'm returning hundreds of records. Looping through each element in the array to pickout two to five elements seems like WAY too much work.

Is there a way to do something like 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 a similar question here, but the answer seems odd to me. There has to be a way....

I hope this helps more

Array / JSON Structure:

Object { search_results: […] }
search_results : Array [ […], […], […], … ]
[0..99]
0 : Array [ "all", "stuff", "I want", … ]
14
  • Are you going one level too far - there's 3 each calls in a 2D array Commented Aug 18, 2017 at 12:57
  • I know I found it odd I had to go that deep, something about how my json gets returned. The first piece of code works, which tells me that is in fact the array I want. I hope it's that simple, I'll give it a shot. Commented Aug 18, 2017 at 12:58
  • I don't really see what the question is - can you explain? You're missing var (or const, let) before first_name and last_name by the way Commented Aug 18, 2017 at 12:59
  • Why use jQuery at all for array manipulation? $.each(search_results is the same as search_results.forEach( Commented Aug 18, 2017 at 13:02
  • 1
    I still don't understand the question. Of course array[3] should work, why wouldn't it? Commented Aug 18, 2017 at 13:13

1 Answer 1

1

From what I understood from your question, you have an 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 you want to access elements 1.3, 1.5, 2.3, 2.5....

Following code will access those 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 you can post your array structure everyone can better understand your question.

Sign up to request clarification or add additional context in comments.

8 Comments

Thank you for the effort, I still have the similar issue though. So now my list didn't populate. This kind of tells me I'm at the wrong level because of the if statement.
@gregnnylf94 If you want to display all of your records, you have to iterate through all of them. What exactly is the issue here?
That's seems crazy to me. The issue is that I am returning a list of 100's of records, each of which has 30 elements in the array. I DO NOT want to loop over 300 + elements to get 60-90 values. It takes way to long on the user side. I just want to grab array[3] array[5] and possible array[50] for example. I do not have the option of limiting my array to just those elements either... (unless I loop through them and put them in another array, even worse)
@gregnnylf94: Did I understand your question correctly?
Yes you are, thank you. I have added the data structure to the question. I'm looking at this code snippet now.
|

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.