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 6855943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:49:20+00:00 2026-05-27T01:49:20+00:00

analyse : function (that) { var a = new Array(); var x = 0;

  • 0
analyse : function (that) {

        var a = new Array();
        var x = 0;

        $(that).children("li").each(function(){
            console.log('test1');
            a[x]['name'] = 'f'; 
            a[x]['link'] = 'UUUUUUUUUUU';
            console.log('test2');
            x++;
        })

        return a;
    }

I’m trying to create an array to store the hierarchy from my menu for PHP later on.

The console won’t show me “test2”, what did I do wrong?


Transformed into this with Didier G’s Help:

analyse : function (that) {
        return $(that).children('li').map(function() {
            var b = {
                name: $(this).children('a').text(), 
                link: $(this).children('a').attr('href')
            };
            if ($(this).children('ul').size() > 0) {
               b.childs =  mcms.module.analyse($(this).children('ul'));
            } 
            return b;
        });
    }

So if i say var y = analyse('#menu'); I get the whole bunch! ^^

  • 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-05-27T01:49:20+00:00Added an answer on May 27, 2026 at 1:49 am

    'a[x]' is undefined at that moment. You have to first build an object and assign it to ‘i’ position (‘x’ is indeed a not standard name for an iterator, thanks @Cito):

    var a = new Array();
    var i = 0;
    
    $(that).children("li").each(function(){
            console.log('test1');
            a[i] = { name: 'f', link: 'UUUU' };
            console.log('test2');
            i++;
    });
    

    Note: your code misses a ; after the each(). Even though it is valid javascript to omit semi-colons, it is I think better to explicitly use them to avoid misinterpretations.


    Creating array can be achieve by using .map()

    var a = [];
    
    // .map() returns a jquery array, to obtain a pure javascript array, you must call .toArray() afterwards
    a = $(that).children('li').map(function() {
        return { name: 'f', link: 'UUUU' };
    }).toArray();
    

    Here’s a jsfiddle to illustrate

    This article covers a the use of .each() and .map() for building data collections out of lists in jquery.

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

Sidebar

Related Questions

How do I enable the MySQL function that logs each SQL query statement received
I want to analyze the result of each function that I call. If this
I am trying to analyse the IIS log and would like to look at
Is there a tool that can analyse my .NET code (C# and VB.NET )
I was thinking of writing a PHP script that would analyse a CMS'd page's
We build a system that handling the billing analyse and what it does it
I'm planning software that's an OLAP application at its heart (it helps analyse metering
I noticed that there are some apps main function is to load the web
There is a JDK function that, although the javadocs does not declare it as
Does the webservice adaptor's function is let different webservice(eg,http/jms) can call each other?Does it

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.