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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:13:29+00:00 2026-06-17T09:13:29+00:00

I am trying to add a property to javascript nested array object… I need

  • 0

I am trying to add a property to javascript nested array object…

I need to traverse the tree get the value of text property and convert it to lowercase and add this data as new property (lowerText)

Old array:

 var oldObject= [{
        text: "Subgroup3",
        items: [{
            text: "subgroup5",
            items: [{
                text: "subgroup6",
                items: [{
                    text: "subgroup7",
                    items: [{
                        text: "subgroup8"
                    }]
                }]
            }]
        }]
    }]

I need the new array object as below:

   var newObject= [{
        text: "Subgroup3",
        lowerText:"subgroup3",
        items: [{
            text: "subgroup5",
            lowerText:"subgroup5",
            items: [{
                text: "subgroup6",
                lowerText:"subgroup6",
                items: [{
                    text: "subgroup7",
                    lowerText:"subgroup7",
                    items: [{
                        text: "subgroup8",
                        lowerText:"subgroup8",
                    }]
                }]
            }]
        }]
    }]

This is what I tried, looping through each object and passing the items (array) to recursive function to set the property but it doesn’t seem to work fine. Not sure what I am doing wrong, can someone please help me with this code?

for (var i = 0; i < data.length; i++) {
                    data[i].lowerText=data[i].text.toLowerCase();
                    loopTree(data[i].items);
                }

function loopTree(node){
 if (node) {
        $.each(node, function (idx, item) {
            item.lowerText=item.text.toLowerCase();
            if(item.items){
                loopTree(item.items)
            }
        });
    }
    }

EDIT: Below code did the job.

  for (var i = 0; i < data.length; i++) {
                    if(data[i]){
                        process( data[i]);
                    }
                }

function process(val) {
    val.lowerText = val.text.toLowerCase();  
    if(val.items){
        for(var i = 0, len = val.items.length; i < len; i++) {
            process(val.items[i]);
        }
    }
}
  • 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-17T09:13:29+00:00Added an answer on June 17, 2026 at 9:13 am

    If you don’t want to clone the objects and just modify the existing ones, try this:

    function process(val) {
       val.lowerText = val.text.toLowerCase();
    
       for(var i = 0, len = val.items.length; i < len; i++) {
           process(val.items[i]);
       }
    }
    
    process(obj);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Javascript expression to define object’s property name? I'm trying to add objects
I am trying to add a namespace method to the Object prototype in javascript.
I am trying to add a custom property to a base form that can
I'm trying to add a IsImage property that I wrote myself to the HttpPostedFile
I am trying to add a dependency to a class property using Unity Configuration
I'm trying add data triggers to the default combobox style so each text item
I'm currently trying to get a better understanding of JavaScript and prototyping. I wanted
I am trying to use JavaScript to add rows to a table dynamically using
I'm trying to use Object.Create in JavaScript. I currently have the following code: var
UPDATED FROM ORIGINAL QUESTION I am trying to learn what a javascript object 'looks

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.