1

I want to cast all object fields to boolean.

function parseBooleanObject (obj) {
  const flagKeys = Object.keys(obj)
  return flagKeys.reduce(
    (newFlags, key) => (newFlags[key] = obj[key] === 'true'), {}
  )
}

const obj = { showVideo: 'true', isStudent: 'false' }
const parsedObj = parseBooleanObject(obj)
console.log('parsedObj: ', parsedObj)

But this code returns false instead of parsing fields to boolean

{ showVideo: true, isStudent: false }

ESCoder
  • 15,431
  • 2
  • 19
  • 42
Henok Tesfaye
  • 8,287
  • 13
  • 47
  • 84
  • 1
    You need to `return newFlags` from your reduce function – haim770 Mar 16 '20 at 12:11
  • 1
    `newFlags[key] = obj[key] === 'true'` will return the value of the assignment, which is a boolean. The next time the `reduce` callback is invoked it's going to use *that* value as `newFlags`, so for iteration 2+, `newFlags` is always a boolean and you're assigning `true[key]` or `false[key`] – VLAZ Mar 16 '20 at 12:11
  • 1
    You can always spread the current object into a new one, and add the additional key: `.reduce((newFlags, key) => ({...newFlags, [key]: obj[key] === 'true'}))` – Nick Parsons Mar 16 '20 at 12:18
  • const castToBool = (obj => Object.keys(obj).forEach(x => obj[x] = !!obj[x])); let a = { a: 1, b: 2, c: 0, d: 'a', e: '', f: 'b' }; castToBool(a); console.log(a); – Umair Khan Mar 16 '20 at 12:23
  • @UmairKhan that works for truthy/falsy values, but doesn't work for parsing OPs data where the values are `"true"` or `"false"` – Nick Parsons Mar 16 '20 at 12:25

4 Answers4

3

Assignment operator returns only the current key which gets assigned to newFlags, you need to return whole newFlags back.

Here i have used , operator to return newFlags after assignment.

function parseBooleanObject (obj) {
  const flagKeys = Object.keys(obj)
  return flagKeys.reduce(
    (newFlags, key) => (newFlags[key] = obj[key] === 'true', newFlags), {}
  )
}

const obj = { showVideo: 'true', isStudent: 'false' }
const parsedObj = parseBooleanObject(obj)
console.log('parsedObj: ', parsedObj)

I will personally have them in two separate line in order to keep readability maintained

function parseBooleanObject(obj) {
  const flagKeys = Object.keys(obj)
  return flagKeys.reduce(
    (newFlags, key) => {
    newFlags[key] = obj[key] === 'true'
     return newFlags
    }, {}
  )
}

const obj = {
  showVideo: 'true',
  isStudent: 'false'
}
const parsedObj = parseBooleanObject(obj)
console.log('parsedObj: ', parsedObj)
Code Maniac
  • 37,143
  • 5
  • 39
  • 60
  • it's worth noting that this is using the comma operator - `(a, b)` evaluates to `b` and not _returning_ a tuple. – Alnitak Mar 16 '20 at 12:13
  • just curious, but if you'd actually use the second syntax, why did your first answer only show the comma operator version? ;) – Alnitak Mar 16 '20 at 12:15
  • @Alnitak sure, but here eventually this is what will happen, since we are using implicit return so eventually it will return `b` – Code Maniac Mar 16 '20 at 12:15
  • @Alnitak thats was just to keep code as close as possible to OP's code, i always bet on simple code :) – Code Maniac Mar 16 '20 at 12:16
1

You haven't return the correct value from your Array.reduce.

Same answer as @Code Maniac, different syntax.

function parseBooleanObject(obj) {
  return Object.keys(obj).reduce((tmp, x) => {
    tmp[x] = obj[x] === 'true';
    
    return tmp;
  }, {});
}

const obj = {
  showVideo: 'true',
  isStudent: 'false'
};

const parsedObj = parseBooleanObject(obj);

console.log('parsedObj: ', parsedObj)
Orelsanpls
  • 22,456
  • 6
  • 42
  • 69
0

you have to return object from the reduce function

   function parseBooleanObject(obj) {
        const flagKeys = Object.keys(obj)
        return flagKeys.reduce(
            (newFlags, key) => {
                (newFlags[key] = obj[key] === 'true');
                return newFlags
            }, {}
        )
    }

    const obj = { showVideo: 'true', isStudent: 'false' }
    const parsedObj = parseBooleanObject(obj)
    console.log('parsedObj: ', parsedObj)
Ahmed Kesha
  • 810
  • 5
  • 11
0

different approach with for...in loop

function parseBooleanObject (obj) {
  const parsed = {};
  for (let key in obj) parsed[key] = obj[key] === 'true';
  return parsed;
}

console.log(
  parseBooleanObject(
    { showVideo: 'true', isStudent: 'false' }
  )
);
Niklas E.
  • 1,848
  • 4
  • 13
  • 25