1

I have an array of cat objects and I have to use the map function to change the "readyForHome" property from a string 'Yes' 'No' to be a boolean value true or false. It will be a part of a function that does various other things to the array.

I'm pretty new to higher order functions and everything I've read on here doesn't seem to work for me. Does anyone have an idea as to how they would approach this?

 var cats = [
  { id: '1', name: 'rupert', readyForHome: 'No', age: 12, personality: ['friendly', 'lazy', 'loving']},
  { id: '2', name: 'mrs fluffy', readyForHome: 'Yes', age: 2, personality: ['affectionate', 'playful', 'shy']},
  { id: '3', name: 'tabitha', readyForHome: 'Yes', age: 4, personality: ['aggressive', 'independent']},
  { id: '4', name: 'lily', readyForHome: 'No', age: 8, personality: ['friendly', 'playful', 'mischievous']},
  { id: '5', name: 'stripe', readyForHome: 'Yes', age: 1, personality: ['haughty', 'independent']},
  { id: '6', name: 'bob', readyForHome: 'Yes', age: 1, personality: ['aggressive', 'antisocial', 'nervous']},
  { id: '7', name: 'jean claude cat damme', readyForHome: 'Yes', age: 11, personality: ['sleepy', 'shy', 'loving']},
  { id: '8', name: 'tilly', readyForHome: 'Yes', age: 4, personality: ['playful', 'social', 'attention-seeking']},
  { id: '9', name: 'milo', readyForHome: 'No', age: 7, personality: ['mischievous', 'friendly']},
  { id: '10', name: 'mr claws', readyForHome: 'Yes', age: 13, personality: ['affectionate', 'shy', 'nervous']},
  { id: '11', name: 'robert zimmercat', readyForHome: 'Yes', age: 3, personality: ['folksy', 'prolific', 'neurotic']}
];

3
  • if(row.readyForHome.toUpperCase() == "YES") row.readyForHome=true; else row.readyForHome = false; Commented Apr 5, 2017 at 13:01
  • Please remember to show what you've tried. Commented Apr 5, 2017 at 13:16
  • Hi guys, Apologies for not replying here sooner, I'm totally new to how this all works. I ended up with this solution that also mapped other aspects of the array... var catMap = arr.map(function (cat) { cat.id = +cat.id cat.readyForHome = cat.readyForHome === 'Yes' ? true : false; cat.name = capitalise(cat.name); cat.yearOfBirth = yearOfCatBirth(cat.age) cat.age = undefined; return cat; }) Commented Apr 23, 2017 at 8:57

3 Answers 3

2

You should use a function, that takes your cat object and change it's readyForHome property accordingly, as a callback for map() method.

That's what you need:

cats = cats.map(function(cat){
     cat.readyForHome.toUpperCase() === "YES" ? cat.readyForHome = true : cat.readyForHome = false;
     return cat;
});

var cats = [{
    id: '1',
    name: 'rupert',
    readyForHome: 'No',
    age: 12,
    personality: ['friendly', 'lazy', 'loving']
  },
  {
    id: '2',
    name: 'mrs fluffy',
    readyForHome: 'Yes',
    age: 2,
    personality: ['affectionate', 'playful', 'shy']
  },
  {
    id: '3',
    name: 'tabitha',
    readyForHome: 'Yes',
    age: 4,
    personality: ['aggressive', 'independent']
  },
  {
    id: '4',
    name: 'lily',
    readyForHome: 'No',
    age: 8,
    personality: ['friendly', 'playful', 'mischievous']
  },
  {
    id: '5',
    name: 'stripe',
    readyForHome: 'Yes',
    age: 1,
    personality: ['haughty', 'independent']
  },
  {
    id: '6',
    name: 'bob',
    readyForHome: 'Yes',
    age: 1,
    personality: ['aggressive', 'antisocial', 'nervous']
  },
  {
    id: '7',
    name: 'jean claude cat damme',
    readyForHome: 'Yes',
    age: 11,
    personality: ['sleepy', 'shy', 'loving']
  },
  {
    id: '8',
    name: 'tilly',
    readyForHome: 'Yes',
    age: 4,
    personality: ['playful', 'social', 'attention-seeking']
  },
  {
    id: '9',
    name: 'milo',
    readyForHome: 'No',
    age: 7,
    personality: ['mischievous', 'friendly']
  },
  {
    id: '10',
    name: 'mr claws',
    readyForHome: 'Yes',
    age: 13,
    personality: ['affectionate', 'shy', 'nervous']
  },
  {
    id: '11',
    name: 'robert zimmercat',
    readyForHome: 'Yes',
    age: 3,
    personality: ['folksy', 'prolific', 'neurotic']
  }
];


cats = cats.map(function(cat) {
  cat.readyForHome.toUpperCase() === "YES" ? cat.readyForHome = true : cat.readyForHome = false;
  return cat;
});
console.log(cats);

Sign up to request clarification or add additional context in comments.

Comments

0

 var cats = [
  { id: '1', name: 'rupert', readyForHome: 'No', age: 12, personality: ['friendly', 'lazy', 'loving']},
  { id: '2', name: 'mrs fluffy', readyForHome: 'Yes', age: 2, personality: ['affectionate', 'playful', 'shy']},
  { id: '3', name: 'tabitha', readyForHome: 'Yes', age: 4, personality: ['aggressive', 'independent']},
  { id: '4', name: 'lily', readyForHome: 'No', age: 8, personality: ['friendly', 'playful', 'mischievous']},
  { id: '5', name: 'stripe', readyForHome: 'Yes', age: 1, personality: ['haughty', 'independent']},
  { id: '6', name: 'bob', readyForHome: 'Yes', age: 1, personality: ['aggressive', 'antisocial', 'nervous']},
  { id: '7', name: 'jean claude cat damme', readyForHome: 'Yes', age: 11, personality: ['sleepy', 'shy', 'loving']},
  { id: '8', name: 'tilly', readyForHome: 'Yes', age: 4, personality: ['playful', 'social', 'attention-seeking']},
  { id: '9', name: 'milo', readyForHome: 'No', age: 7, personality: ['mischievous', 'friendly']},
  { id: '10', name: 'mr claws', readyForHome: 'Yes', age: 13, personality: ['affectionate', 'shy', 'nervous']},
  { id: '11', name: 'robert zimmercat', readyForHome: 'Yes', age: 3, personality: ['folksy', 'prolific', 'neurotic']}
].map( cat => {
cat.readyForHome = (cat.readyForHome === 'Yes'); // true only if 'Yes'
return cat;
});
console.log(cats);

Comments

0
  var catMap = arr.map(function (cat) {
  cat.readyForHome = cat.readyForHome === 'Yes' ? true : false;
    return cat;

})

This is what I ended up with. Apologies for not posting what I'd hacked about with earlier. I'm new to usuing Stack Overflow and to be honest I'd kind of forgotten I'd posted the question! Thank you all for taking the time to answer :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.