Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

JavaScript loop through multidimensional array of object

I have this variable:

let obj = {
    "aff_link": "https://event.2performant.com/events/click?ad_type=product_store&unique=83b281931&aff_code=d79aaed64&campaign_unique=2e49eab4f",
    "availability": true,
    "brand": "Casio",
    "date_add": 1666792631,
    "date_upd": 1666792631,
    "id": 78594,
    "price": 159.24,
    "product_type": "Ceasuri de mana",
    "title": "Ceas Casio CLASSIC LTP-1303L-7BVEF",
    "update_history": [
        {
            "id_feed": 108,
            "date_upd": 1666794541,
            "address" : [
                {
                    "present" :  "Dhanmondi 15",
                    "permanent" : "Gulshan 2"  
                }
            ]
        }
    ],
    "vgt_id": 0,
    "originalIndex": 0
};

Now, I have an array called:

let arr = [];

Now Using below function I loop through all the property and store it to variable arr.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

function eachRecursive(obj) {
    for (var k in obj)  {
        if( k !== 'additional_image_link' ) {
            if (typeof obj[k] == "object" && obj[k] !== null) {
                eachRecursive(obj[k]);            
            } else {
                arr.push({
                    [k] : obj[k]
                })              
            }
        }
    }
}

Now, usinge console.log( arr ) I got this output:

[ { aff_link:
     'https://event.2performant.com/events/click?ad_type=product_store&unique=83b281931&aff_code=d79aaed64&campaign_unique=2e49eab4f' },
  { availability: true },
  { brand: 'Casio' },
  { date_add: 1666792631 },
  { date_upd: 1666792631 },
  { id: 78594 },
  { price: 159.24 },
  { product_type: 'Ceasuri de mana' },
  { title: 'Ceas Casio CLASSIC LTP-1303L-7BVEF' },
  { id_feed: 108 },
  { date_upd: 1666794541 },
  { present: 'Dhanmondi 15' },
  { permanent: 'Gulshan 2' },
  { vgt_id: 0 },
  { originalIndex: 0 } 
]

if you look at it you can see the there is no key called update_history, right ? I want that above output should contain update_history as a key and the value should contain id_feed, date_upd, present, permanent with their corresponding value.

Can you help me please?

>Solution :

update_history is an object (arrays are objects too), so your code recurses through it (hence you have all the subkeys of update_history in your output). You can prevent that by checking that obj[k] is not an array before recursing:

let obj = {
    "aff_link": "https://event.2performant.com/events/click?ad_type=product_store&unique=83b281931&aff_code=d79aaed64&campaign_unique=2e49eab4f",
    "availability": true,
    "brand": "Casio",
    "date_add": 1666792631,
    "date_upd": 1666792631,
    "id": 78594,
    "price": 159.24,
    "product_type": "Ceasuri de mana",
    "title": "Ceas Casio CLASSIC LTP-1303L-7BVEF",
    "update_history": [
        {
            "id_feed": 108,
            "date_upd": 1666794541,
            "address" : [
                {
                    "present" :  "Dhanmondi 15",
                    "permanent" : "Gulshan 2"  
                }
            ]
        }
    ],
    "vgt_id": 0,
    "originalIndex": 0
};

let arr = [];

function eachRecursive(obj) {
    for (var k in obj)  {
        if( k !== 'additional_image_link' ) {
            if (typeof obj[k] == "object" && !Array.isArray(obj[k]) && obj[k] !== null) {
                eachRecursive(obj[k]);            
            } else {
                arr.push({
                    [k] : obj[k]
                })              
            }
        }
    }
}

eachRecursive(obj)

console.log(arr)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading