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

Time:05-05

I have an array with an object nested in it. I'm trying to use map() to create an array that has in each index the string "war" or "peace". The string "war" if the name property has both a cat and a dog. "peace" otherwise. What I've tried so far is too have a nested for in loop to access the nested object in the pets object. But I can't get it to check all of the name properties. Here's my code.

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

CodePudding user response:

I think what you're trying to do is check the list of pets to see if it contains a "dog" type and a "cat" type for any given person. In that case, what you can do is map the pet types into a Set (to get a list of unique pet types) for each person, and then check whether that Set contains both "cat" and "dog". If so, return "war", if not, return "peace". Here's an example:

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

CodePudding user response:

One way would be to map the data array, check to see if pets array has both cat and dog types using array.some() method, and return war or peace accordingly:

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>

  • Related