1

I have this array of objects from an api:

"use strict";

var data = [{
  "OPEN COVER (YES or NO)": "YES"
}, {
  "OPEN COVER (YES or NO)": "NO"
}, {
  "OPEN COVER (YES or NO)": "YES"
}];
var result = [];

data.map(function (item) {
  var fixed = {};
  var keys = Object.keys(item);
  keys.map(function (key) {
    if (key === 'OPEN COVER (YES or NO)') return fixed['open_cover'] = item[key];
  });
  result.push(fixed);
});
console.log(result);

How do i change the value from "YES" to true and "NO" to true?

4 Answers 4

3

You can easily convert a 'YES' to true and 'NO' to false using a ternary. Also, you can use a forEach instead of a map:

"use strict";

var data = [{
  "OPEN COVER (YES or NO)": "YES"
}, {
  "OPEN COVER (YES or NO)": "NO"
}, {
  "OPEN COVER (YES or NO)": "YES"
}];
var result = data.reduce(function(items, item) {
  var fixed = {};
  var keys = Object.keys(item);
  keys.forEach(function(key) {
    if (key === 'OPEN COVER (YES or NO)') {
      return fixed['open_cover'] = item[key] === 'YES';
    }
  });
  items.push(fixed);
  return items;
}, []);
console.log(result);

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

5 Comments

Better is item[key] === 'YES' without the ternary, since this is already a boolean expression yielding a false or true.
@trincot Yes! Updated.
@ChaseDeAnda if you are not returning anything in the map, you better use forEach. map() is used to iterate and create a new array based on a previous one. I have made an answer, based on your original one, using map.
@ChristianBenseler Updated to use reduce.
I think that using reduce to do this adds a lot of complexity, although it works. In the scenario described, where there is an array and it must returns a new one, use map() is more suited, because creates a new array with the results of calling a provided function on every element in the calling array. The reduce method is usually used to reduce an array to a single value.
2

If you could use ES6 features, use map to iterates and returns a new array, creating a new object with spread operator for each array item

"use strict";

var data = [{
  "OPEN COVER (YES or NO)": "YES"
}, {
  "OPEN COVER (YES or NO)": "NO"
}, {
  "OPEN COVER (YES or NO)": "YES"
}];

const key = 'OPEN COVER (YES or NO)';

const result = data.map( item => {

  return {
    ...item,
    ...{
        [key]: item[key] === 'YES'
    }
  }
});
console.log(result);

Working fiddle, based on Chase DeAnda's original answer https://jsfiddle.net/xctohpw5/

Comments

0

Or something like this, assuming that you will always have fixed key name "OPEN COVER (YES or NO)":

"use strict";

var data = [{
  "OPEN COVER (YES or NO)": "YES"
}, {
  "OPEN COVER (YES or NO)": "NO"
}, {
  "OPEN COVER (YES or NO)": "YES"
}];
var result = data.map(function (item) {
 var tempObj = {};
 var key = Object.keys(item)[0];
 var value = item[key] === 'YES';

 tempObj[key] = value;

 return tempObj;
});

console.log(result);

Comments

-1

The easiest way to do this would be to set the value to a simple statement.

fixed['open_cover'] = item[key] === "YES"

This will check if item[key] is equal to "YES" and set your output to true or false.

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.