0

I have an array of arrays, each having a series of list items, which I am logging with console.log(jobsList); The output is:

enter image description here

The variable jobsList is created by pushing a bunch of arrays created in a for loop like so:

for (var i=0; i < teamList.length; i++ ) {
  jobDirectory.push(newJob);
} 
jobsList.push(jobDirectory);

We are then sorting the arrays by length like so:

jobsList.sort(function(a,b){
  return a.length < b.length;
});

I need to output each array inside of an unordered list. The desired output is like so:

<h3></h3><ul>//insert list items here</ul>

This would be repeated for every top level array.

$('.row').append(jobsList); throws an error. How do I return all of the arrays inside an unordered list like above?

6
  • h3 is not allowed inside ul. You should place it before your h3. Commented Feb 23, 2015 at 18:18
  • 1
    As a side note, you can't have anything but <li> elements as children of <ul>, so your <h3> will need a new home. Commented Feb 23, 2015 at 18:18
  • 1
    Can you post your full code here? And what error is occurring with it? Commented Feb 23, 2015 at 18:18
  • of course, that was a typo, but not important to this question Commented Feb 23, 2015 at 18:18
  • @ChrisPietschmann sure, Ill update Commented Feb 23, 2015 at 18:19

3 Answers 3

1

You can use:

for (var i in jobsList)
    $('.row').append($('<ul />').append(jobsList[i]));

The reason your code doesn't work is because jobsList is not an array of elements but an array of array of elements. This way you can add these arrays of elements one by one.

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

3 Comments

In the doc it says jquery accepts a list of elements
for (var i in jobsList) { $('.row').append('<ul>' + jobsList[i] + '</ul>'); } results in comma separated list items
Does $('.row').append($('<ul />').append(jobsList[i])); also work?
0

Use a combination of forEach and join this way.

jobsList.forEach(function(e){

   $(".container").append("<ul>"+e.join("")+"</ul>");

});

Where container is the class of parent div of ul. If their is no parent of ul then use body instead.

Comments

0

Please use the following code:

$.each(jobsList, function () {
    var $ul = $('<ul/>');
    var $row = $('.row');

    $.each(this, function () {
        $ul.append($(this));
    });

    $row.append('<h3>some title</h3>')
    $row.append($ul);
});

1 Comment

Had trouble with this, but I like using $.each. Error is: Uncaught TypeError: Cannot read property 'ownerDocument' of undefined

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.