将数据复制到对象的嵌套数组的数组中

Copy data in array of a nested array of an object

所以伙计们,我不知道如何给这个主题。我无法将数据从对象数组复制到另一个新创建的数组。

例如,我想复制并创建一个新的array,其中包含我数据库中每个人的所有动物类别。

 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" }
         ]
     }
 ]

因此,我创建了一个名为 animalCategory 的新 array 来保存所有可用的类别。

 // 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 {
            // 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)

但不幸的是,结果我得到了一大堆 animalCategory。还有很多重复动物类。 (如下图)

UPDATED:我要寻找的输出是:-

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

那我该如何改变我的逻辑呢?还有其他方法可以做到这一点吗?

您可以尝试使用 Set。集合仅由唯一值组成。

如果您压入相同的值并不重要,只是不会将其添加到集合中。

如果您想将类别转换回数组,请使用 Array.from 方法并传入集合。

let animalCategory = new Set()
people.forEach(person => {
    // go and loop inside animals array
    person.animals.forEach(animal => {
          animalCategory.add(animal.category)
    })
})
animalCategory = Array.from(animalCategory)

下面是一个示例,它获得了您要查找的输出并删除了重复项。

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" }
         ]
     }
 ];
 
 const uniqAnimals = [...new Set(people.flatMap(p => p.animals).map(a => a.category))];
 
 console.log(uniqAnimals);

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);
});

希望对您有所帮助。

这是另一个解决方案。

people.forEach(people => {
   if (people.animals && people.animals.length) {
       people.animals.forEach(categories => {
           if (animalCategories.indexOf(categories.category) === -1) {
               animalCategories.push(categories.category);
           }
       });
   }
});

我们可以收集数组中所有类别 属性 的值,然后从中删除重复值。

var 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"
    }]
  }
];
var getArr = [];
var animalsArr = people.map(x => x.animals.map(y => {
  getArr.push(y.category);
}));

var filteredArr = getArr.filter((value, index, self) => self.indexOf(value) === index);
console.log(filteredArr);