如何根据数据类型创建一个在每个索引中都有一个字符串的数组?

How to create an array that has in each index a string depending on the data type?

我有一个数组,其中嵌套了一个对象。我正在尝试使用 map() 创建一个数组,每个索引中都有字符串 "war""peace"。如果 name 属性 同时具有 catdog,则字符串“war”。 "peace" 否则。到目前为止,我已经尝试过使用嵌套的 for in 循环来访问 pets 对象中的嵌套对象。但我无法让它检查所有 name 属性。这是我的代码。

const data = [
    {
        name: 'mark',
        age: 27,
        from: "Chicago",
        pets: []
    }, {
        name: 'greg',
        age: 68,
        from: "Auburn",
        pets: [{
            type: "rock",
            name: "herbert",
            age: 126003219
        }]
    }, {
        name: 'ken',
        age: 28,
        from: "Naperville",
        pets: [{
            type: "cat",
            name: "colby",
            age: 1
        }, {
            type: "dog",
            name: "charlie",
            age: 3
        }, {
            type: "dog",
            name: "william riker",
            age: 1
        }, {
            type: "dog",
            name: "thomas riker",
            age: 1
        }]
    }, {
        name: 'kristiana',
        age: 47,
        from: "Chicago",
        pets: []
    }, {
        name: 'bryan',
        age: 27,
        from: "Lincoln Park Zoo",
        pets: [{
            type: "white tiger",
            name: "arthur",
            age: 3
        }, {
            type: "silverback gorilla",
            name: "tommy",
            age: 4
        }, {
            type: "guinea pig",
            name: "kirk",
            age: 1
        }, {
            type: "hamster",
            name: "spock",
            age: 1
        }, {
            type: "hamster",
            name: "sarek",
            age: 1
        }, {
            type: "cat",
            name: "quark",
            age: 1
        }, {
            type: "cat",
            name: "kira nerys",
            age: 1
        }]
    }, {
        name: 'anthony',
        age: 31,
        from: "Phoenix",
        pets: []
    }, {
        name: 'stephanie',
        age: 27,
        from: "Chicago",
        pets: [{
            type: "dog",
            name: "midnight",
            age: 3
        }]
    }, {
        name: 'david p',
        age: 40,
        from: "Lancaster",
        pets: [{
            type: "dog",
            name: "quinn",
            age: 8
        }]
    }, {
        name: 'patrick',
        age: 39,
        from: "Poway",
        pets: [{
            type: "dog",
            name: "hazel",
            age: 7
        }, {
            type: "cat",
            name: "fredy perlman",
            age: 13
        }]
    }, {
        name: 'david a',
        age: 32,
        from: "San Francisco",
        pets: [{
            type: "monstera",
            name: "holey",
            age: 1
        }]
    }, {
        name: 'timm',
        age: 32,
        from: "Gainsville",
        pets: [{
            type: "goldfish",
            name: "betsy",
            age: 4
        }]
    }, {
        name: 'conrad',
        age: 101,
        from: "Minneapolis",
        pets: []
    }, {
        name: 'nate',
        age: 14,
        from: "Boston",
        pets: [{
            type: "cat",
            name: "rosie",
            age: 0.005
        }, {
            type: "hermit crab",
            name: "kermit",
            age: 1 // 16 wow wtf
        }, {
            type: "dog",
            name: "ernie",
            age: 4
        }, {
            type: "cat",
            name: "kendrick lamar jr",
            age: 6
        }, {
            type: "cat",
            name: "guinan",
            age: 589
        }, {
            type: "cat",
            name: "beverly",
            age: 1
        }]
    }, {
        name: 'temo',
        age: 25,
        from: "Osaka",
        pets: [{
            type: "dog",
            name: "nacho",
            age: 1
        }, {
            type: "dog",
            name: "belky",
            age: 1
        }, {
            type: "dog",
            name: "janeway",
            age: 4
        }, {
            type: "dog",
            name: "jean-luc",
            age: 12
        }, {
            type: "monkey",
            name: "shin chan",
            age: 1
        }]
    }, {
        name: 'griffin',
        age: 22,
        from: "Chicago",
        pets: [{
            type: "bearded dragon",
            name: "ferg",
            age: 3
        }]
    }, {
        name: 'eman',
        age: 20,
        from: "Lake in the Hills",
        pets: [{
            type: "parakeet",
            name: "sunny",
            age: 400
        }, {
            type: "parrot",
            name: "vic fontaine",
            age: 46
        }, {
            type: "parrot",
            name: "worf",
            age: 48
        }, {
            type: "cockatoo",
            name: "spockatoo",
            age: 33
        }]
    }, {
        name: 'jürgen',
        age: 28,
        from: "Chicago",
        pets: [{
            type: "cat",
            name: "mija",
            age: 6
        }]
    }, {
        name: 'michele',
        age: 33,
        from: "Rome",
        pets: [{
            type: "cat",
            name: "caesar", // "cheeto"
            age: 3
        }, {
            type: "cat",
            name: "cleopatra",
            age: 3
        }, {
            type: "cat",
            name: "tuvok",
            age: 10
        }, {
            type: "turtle",
            name: "spock",
            age: 13
        }]
    }, {
        name: 'mitchell',
        age: 19,
        from: "Iowa City",
        pets: [{
            type: "cat",
            name: "eddie",
            age: 7
        }]
    }, {
        name: 'reuben',
        age: 41,
        from: "Durham",
        pets: [{
            type: "cat",
            name: "nico",
            age: 15
        }, {
            type: "cat",
            name: "rocky",
            age: 5
        }]
    }, {
        name: 'fatima',
        age: 24,
        from: "Milwaukee",
        pets: [{
            type: "snake",
            name: "reginald",
            age: 12
        }]
    }
]


const warAndPeace = data.map(sadPeople => {
  for (keys in sadPeople) {
    for (key in sadPeople) {
      if (sadPeople['pets'].type === 'dog ' && sadPeople === 'cat') {
        console.log('War');
      } else {
        console.log('peace');
      }
    }
  }
});

console.log(warAndPeace);

我认为您想要做的是检查宠物列表,看它是否包含任何给定人的“狗”类型和“猫”类型。在这种情况下,您可以做的是将每个人的宠物类型映射到 Set(以获取唯一宠物类型列表),然后检查该 Set 是否包含 both “猫”和“狗”。如果是,return“war”,如果不是,return“和平”。这是一个例子:

const data = [ { name: 'mark', age: 27, from: "Chicago", pets: [] }, { name: 'greg', age: 68, from: "Auburn", pets: [{ type: "rock", name: "herbert", age: 126003219 }] }, { name: 'ken', age: 28, from: "Naperville", pets: [{ type: "cat", name: "colby", age: 1 }, { type: "dog", name: "charlie", age: 3 }, { type: "dog", name: "william riker", age: 1 }, { type: "dog", name: "thomas riker", age: 1 }] }, { name: 'kristiana', age: 47, from: "Chicago", pets: [] }, { name: 'bryan', age: 27, from: "Lincoln Park Zoo", pets: [{ type: "white tiger", name: "arthur", age: 3 }, { type: "silverback gorilla", name: "tommy", age: 4 }, { type: "guinea pig", name: "kirk", age: 1 }, { type: "hamster", name: "spock", age: 1 }, { type: "hamster", name: "sarek", age: 1 }, { type: "cat", name: "quark", age: 1 }, { type: "cat", name: "kira nerys", age: 1 }] }, { name: 'anthony', age: 31, from: "Phoenix", pets: [] }, { name: 'stephanie', age: 27, from: "Chicago", pets: [{ type: "dog", name: "midnight", age: 3 }] }, { name: 'david p', age: 40, from: "Lancaster", pets: [{ type: "dog", name: "quinn", age: 8 }] }, { name: 'patrick', age: 39, from: "Poway", pets: [{ type: "dog", name: "hazel", age: 7 }, { type: "cat", name: "fredy perlman", age: 13 }] }, { name: 'david a', age: 32, from: "San Francisco", pets: [{ type: "monstera", name: "holey", age: 1 }] }, { name: 'timm', age: 32, from: "Gainsville", pets: [{ type: "goldfish", name: "betsy", age: 4 }] }, { name: 'conrad', age: 101, from: "Minneapolis", pets: [] }, { name: 'nate', age: 14, from: "Boston", pets: [{ type: "cat", name: "rosie", age: 0.005 }, { type: "hermit crab", name: "kermit", age: 1 }, { type: "dog", name: "ernie", age: 4 }, { type: "cat", name: "kendrick lamar jr", age: 6 }, { type: "cat", name: "guinan", age: 589 }, { type: "cat", name: "beverly", age: 1 }] }, { name: 'temo', age: 25, from: "Osaka", pets: [{ type: "dog", name: "nacho", age: 1 }, { type: "dog", name: "belky", age: 1 }, { type: "dog", name: "janeway", age: 4 }, { type: "dog", name: "jean-luc", age: 12 }, { type: "monkey", name: "shin chan", age: 1 }] }, { name: 'griffin', age: 22, from: "Chicago", pets: [{ type: "bearded dragon", name: "ferg", age: 3 }] }, { name: 'eman', age: 20, from: "Lake in the Hills", pets: [{ type: "parakeet", name: "sunny", age: 400 }, { type: "parrot", name: "vic fontaine", age: 46 }, { type: "parrot", name: "worf", age: 48 }, { type: "cockatoo", name: "spockatoo", age: 33 }] }, { name: 'jürgen', age: 28, from: "Chicago", pets: [{ type: "cat", name: "mija", age: 6 }] }, { name: 'michele', age: 33, from: "Rome", pets: [{ type: "cat", name: "caesar", age: 3 }, { type: "cat", name: "cleopatra", age: 3 }, { type: "cat", name: "tuvok", age: 10 }, { type: "turtle", name: "spock", age: 13 }] }, { name: 'mitchell', age: 19, from: "Iowa City", pets: [{ type: "cat", name: "eddie", age: 7 }] }, { name: 'reuben', age: 41, from: "Durham", pets: [{ type: "cat", name: "nico", age: 15 }, { type: "cat", name: "rocky", age: 5 }] }, { name: 'fatima', age: 24, from: "Milwaukee", pets: [{ type: "snake", name: "reginald", age: 12 }] } ]


let warAndPeace = data.map(person => {
  let petTypes = new Set(person.pets.map(pet => pet.type));
  if (petTypes.has('dog') && petTypes.has('cat')) {
    return 'war'
  }
  return 'peace'
});

console.log(warAndPeace);

一种方法是映射 data 数组,使用 array.some() 方法检查 pets 数组是否同时具有 catdog 类型, 和 return warpeace 相应地:

let result = data.map(i =>
  i.pets.some(p => p.type === 'cat') && i.pets.some(p => p.type === 'dog') ? 'war' : 'peace'
)

console.log(result)
<script>
  const data = [
    {
        name: 'mark',
        age: 27,
        from: "Chicago",
        pets: []
    }, {
        name: 'greg',
        age: 68,
        from: "Auburn",
        pets: [{
            type: "rock",
            name: "herbert",
            age: 126003219
        }]
    }, {
        name: 'ken',
        age: 28,
        from: "Naperville",
        pets: [{
            type: "cat",
            name: "colby",
            age: 1
        }, {
            type: "dog",
            name: "charlie",
            age: 3
        }, {
            type: "dog",
            name: "william riker",
            age: 1
        }, {
            type: "dog",
            name: "thomas riker",
            age: 1
        }]
    }, {
        name: 'kristiana',
        age: 47,
        from: "Chicago",
        pets: []
    }, {
        name: 'bryan',
        age: 27,
        from: "Lincoln Park Zoo",
        pets: [{
            type: "white tiger",
            name: "arthur",
            age: 3
        }, {
            type: "silverback gorilla",
            name: "tommy",
            age: 4
        }, {
            type: "guinea pig",
            name: "kirk",
            age: 1
        }, {
            type: "hamster",
            name: "spock",
            age: 1
        }, {
            type: "hamster",
            name: "sarek",
            age: 1
        }, {
            type: "cat",
            name: "quark",
            age: 1
        }, {
            type: "cat",
            name: "kira nerys",
            age: 1
        }]
    }, {
        name: 'anthony',
        age: 31,
        from: "Phoenix",
        pets: []
    }, {
        name: 'stephanie',
        age: 27,
        from: "Chicago",
        pets: [{
            type: "dog",
            name: "midnight",
            age: 3
        }]
    }, {
        name: 'david p',
        age: 40,
        from: "Lancaster",
        pets: [{
            type: "dog",
            name: "quinn",
            age: 8
        }]
    }, {
        name: 'patrick',
        age: 39,
        from: "Poway",
        pets: [{
            type: "dog",
            name: "hazel",
            age: 7
        }, {
            type: "cat",
            name: "fredy perlman",
            age: 13
        }]
    }, {
        name: 'david a',
        age: 32,
        from: "San Francisco",
        pets: [{
            type: "monstera",
            name: "holey",
            age: 1
        }]
    }, {
        name: 'timm',
        age: 32,
        from: "Gainsville",
        pets: [{
            type: "goldfish",
            name: "betsy",
            age: 4
        }]
    }, {
        name: 'conrad',
        age: 101,
        from: "Minneapolis",
        pets: []
    }, {
        name: 'nate',
        age: 14,
        from: "Boston",
        pets: [{
            type: "cat",
            name: "rosie",
            age: 0.005
        }, {
            type: "hermit crab",
            name: "kermit",
            age: 1 // 16 wow wtf
        }, {
            type: "dog",
            name: "ernie",
            age: 4
        }, {
            type: "cat",
            name: "kendrick lamar jr",
            age: 6
        }, {
            type: "cat",
            name: "guinan",
            age: 589
        }, {
            type: "cat",
            name: "beverly",
            age: 1
        }]
    }, {
        name: 'temo',
        age: 25,
        from: "Osaka",
        pets: [{
            type: "dog",
            name: "nacho",
            age: 1
        }, {
            type: "dog",
            name: "belky",
            age: 1
        }, {
            type: "dog",
            name: "janeway",
            age: 4
        }, {
            type: "dog",
            name: "jean-luc",
            age: 12
        }, {
            type: "monkey",
            name: "shin chan",
            age: 1
        }]
    }, {
        name: 'griffin',
        age: 22,
        from: "Chicago",
        pets: [{
            type: "bearded dragon",
            name: "ferg",
            age: 3
        }]
    }, {
        name: 'eman',
        age: 20,
        from: "Lake in the Hills",
        pets: [{
            type: "parakeet",
            name: "sunny",
            age: 400
        }, {
            type: "parrot",
            name: "vic fontaine",
            age: 46
        }, {
            type: "parrot",
            name: "worf",
            age: 48
        }, {
            type: "cockatoo",
            name: "spockatoo",
            age: 33
        }]
    }, {
        name: 'jürgen',
        age: 28,
        from: "Chicago",
        pets: [{
            type: "cat",
            name: "mija",
            age: 6
        }]
    }, {
        name: 'michele',
        age: 33,
        from: "Rome",
        pets: [{
            type: "cat",
            name: "caesar", // "cheeto"
            age: 3
        }, {
            type: "cat",
            name: "cleopatra",
            age: 3
        }, {
            type: "cat",
            name: "tuvok",
            age: 10
        }, {
            type: "turtle",
            name: "spock",
            age: 13
        }]
    }, {
        name: 'mitchell',
        age: 19,
        from: "Iowa City",
        pets: [{
            type: "cat",
            name: "eddie",
            age: 7
        }]
    }, {
        name: 'reuben',
        age: 41,
        from: "Durham",
        pets: [{
            type: "cat",
            name: "nico",
            age: 15
        }, {
            type: "cat",
            name: "rocky",
            age: 5
        }]
    }, {
        name: 'fatima',
        age: 24,
        from: "Milwaukee",
        pets: [{
            type: "snake",
            name: "reginald",
            age: 12
        }]
    }
]
</script>