Search code examples
javascriptarrayscopyarrayobjectnested-object

Copy data in array of a nested array of an object


So guys, I don't how to give a topic to this one. I'm having trouble to copy data from array of Object to another newly created array.

For example, I want to copy and create a new array that contain all category of animals of every person in my database.

 people = [ 
     {
         name: "Person 1",
         animals: [
             { category: "cat" },
             { category: "dog" },
             { category: "fish" }
         ]
     },
     {
         name: "Person 2",
         animals: [
             { category: "dog" },
             { category: "iguana" }
         ]
     },
     {
         name: "Person 3",
         animals: [
             { category: "cat" }
         ]
     }
 ]

So, I created a new array named animalCategory to hold every category available.

 // declare new array to hold category of animals
 let animalCategory = []

This is the logic I came up with:-

// loop all person available
people.forEach(person => {
    // go and loop inside animals array
    person.animals.forEach(animal => {
        // save new category of animals if animalCategory array is EMPTY
        if(animalCategory.length === 0) {
            animalCategory.push(animal.category)
        }

        // if NOT EMPTY, then
        else {
            // loop and check existing animal categories in animalCategory array
            animalCategory.forEach(category => {
                // check if MATCH?
                if(category === animal.category) {
                    break // or just continue or will NOT BE SAVE
                }

                // if NOT MATCH, then
                else {
                    // SAVE new category
                    animalCategory.push(animal.category)
                }
            })
        }
    })
})

// see result
console.log(animalCategory.length)

But unfortunately I got like a very big array of animalCategory as a result. And a lot of repetative animals category. (like shown below)

enter image description here

UPDATED: the output I want to seek is:-

animalCategory: [ 'cat', 'dog', 'iguana', 'fish']

How should I change my logic then? And is there any other way I can do this?


Solution

  • people = [ 
         {
             name: "Person 1",
             animals: [
                 { category: "cat" },
                 { category: "dog" },
                 { category: "fish" }
             ]
         },
         {
             name: "Person 2",
             animals: [
                 { category: "dog" },
                 { category: "iguana" }
             ]
         },
         {
             name: "Person 3",
             animals: [
                 { category: "cat" }
             ]
         }
     ]
    
    // declare new array to hold category of animals
     let animalCategory = []
    
    
    // loop all person available
    people.forEach(person => {
        // go and loop inside animals array
        person.animals.forEach(animal => {
            // save new category of animals if animalCategory array is EMPTY
            if(animalCategory.length === 0) {
                animalCategory.push(animal.category)
            }
    
            // if NOT EMPTY, then
            else {
                if(animalCategory.indexOf(animal.category) === -1) {
                
                animalCategory.push(animal.category);
                }
            }
        });
    });
    
    // see result
    animalCategory.forEach(function(animal) {
        console.log(animal);
    });

    Hope this is helpfull.