1

所以,伙计们,我不知道如何给这个话题提供一个话题。我无法将数据从 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" }
         ]
     }
 ]

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

 // 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。和很多重复的动物类别。(如下图所示)

在此处输入图像描述

更新:我想要寻求的输出是: -

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

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

4

5 回答 5

1

这是一个示例,该示例可获取您正在寻找的输出并删除了重复项。

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

于 2020-05-29T04:27:17.897 回答
1

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

希望这会有所帮助。

于 2020-05-29T04:30:25.447 回答
0

您可以尝试使用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)
于 2020-05-29T04:25:30.863 回答
0

那是另一种解决方案。

people.forEach(people => {
   if (people.animals && people.animals.length) {
       people.animals.forEach(categories => {
           if (animalCategories.indexOf(categories.category) === -1) {
               animalCategories.push(categories.category);
           }
       });
   }
});
于 2020-05-29T04:34:35.293 回答
0

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

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);
于 2020-05-29T04:45:32.293 回答