Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7892445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:52:17+00:00 2026-06-03T06:52:17+00:00

I’m attempting to create a simple column view which will allow me to explore

  • 0

I’m attempting to create a simple column view which will allow me to explore nested arrays. The goal is to have a user click on a ‘name’ in the ‘name’ column, and have the corresponding movies list appear in the next column. For example, I’d provide a JSON array like this:

var array = [
    {
        "name":"sean connery",
        "index":0,
        "movies": [
            {"name": "Goldfinger",
             "id": 43},
            {"name":"Thunderball",
             "id":103}
            ]
    },{
      "name":"pierce brosnan",
        "index":1,
        "movies": [
            {"name": "Goldeneye",
             "id": 22},
            {"name":"The world is not enough",
             "id":100}
            ]  

    }
    ] 

And basically, I’m kinda cheating in order to retrieve the movies array for the corresponding name. I read the ‘index’ attribute that is appended to the li element, and use that to pick out the appropriate index to retrieve the movies array.

// sorts alphabetically 
array.sort(function(a, b) {
    return b.name < a.name ? 1 : b.name > a.name ? -1 : 0;
});

$.each(array, function(index, value){
   listitem = document.createElement('li');
$(listitem).html(value.name).appendTo($("ul#list")).attr("data-id",value.index);        
});

//make it a scrollbox
$("ul#list").scrollTop(); 

//click to display movies
$("ul#list li").click(function(){
 var id = $(this).attr("data-id"); 
movies = array[id].movies;
    console.log(movies); 
});​

But this doesn’t seem like the ideal method here. It seems like there should be a way to retrieve the appropriate movies array without needing to alter the DOM. Any suggestions? Or am what I’m doing a legit way?

Thanks! and a Fiddle containing my code: http://jsfiddle.net/bmcmahen/WXvWv/2/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T06:52:19+00:00Added an answer on June 3, 2026 at 6:52 am

    You could go even further and attach the whole movie array to the <li> using data:

    $.each(array, function(index, value) {
        var $li = $('<li>').html(value.name).data('movie', value.movies);
        $('ul#list').append($li);
    });
    
    $("ul#list li").click(function() {
        console.log($(this).data('movie'));
    });​
    

    Demo: http://jsfiddle.net/ambiguous/Ug43b/

    Or, if that doesn’t make sense in your situation you can use index to figure out where the <li> is inside the <ul> and use that to index into the array:

    $.each(array, function(index, value) {
        var $li = $('<li>').html(value.name)
        $('ul#list').append($li);
    });
    
    $("ul#list li").click(function() {
        var i = $('ul#list li').index(this);
        console.log(array[i].movies);
    });​
    

    You could also cache $('ul#list li') if you wanted:

    var $lis = $('ul#list li');
    $lis.click(function() {
        var i = $lis.index(this);
        console.log(array[i].movies);
    });
    

    Demo: http://jsfiddle.net/ambiguous/kbvGf/

    Which way you go depends on your specific situation of course. The index approach does require your <ul> to exactly match array and that could be a problem, using a data-attribute nicely sidesteps this problem by binding each <li> directly to a set of movies either directly through data-movies or indirectly through the index in data-id.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a text area in my form which accepts all possible characters from
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.