0

I would like to filter all boolean values from an json object in javascript. I managed to get it working on a simple json object but in my case I have got a nested one. Here is an example of the object. I dont know how to manage this structure.

{  
    "User1":
     {  
      "age":15,
      "country":"sw",
      "glasses":true,
      "car":true,
      "zip":1223,
      "name":"Frank"
    },
    "User2":
     {  
      "age":23,
      "country":"fr",
      "glasses":false,
      "car":true,
      "zip":5577,
      "name":"Mike"
    }
}

And as a result I would like to receive something like this:

{  
   "User1":{  
      "glasses":true,
      "car":true
   },
   "User2":{  
      "glasses":false,
      "car":true
   }
}

5 Answers 5

1
 const result = {};

 for(const [key, value] of Object.entries(input)) {
   const {glasses, car} = value;
   result[key] = {glasses, car};
}

Just iterate over the objects entries and make some destructuring on the inner objects. Or if you really want every boolean:

 const result = {};

 for(const [key, value] of Object.entries(input)) {
   const out = result[key] = {};
   for(const [innerKey, innerValue] of Object.entries(value)) {
     if(typeof innerValue === "boolean")
       out[innerKey] = innerValue;
   }
 }
Sign up to request clarification or add additional context in comments.

Comments

1

For actual JSON string, the JSON.parse reviver parameter can be used to exclude or modify values:

var j = '{"User1":{"age":15,"country":"sw","glasses":true,"car":true,"zip":1223,"name":"Frank"},"User2":{"age":23,"country":"fr","glasses":false,"car":true,"zip":5577,"name":"Mike"}}';

var o = JSON.parse(j, (k, v) => typeof v == 'number' || typeof v == 'string' ? void 0 : v);

console.log(o);

Comments

0

If you don't know which keys will be booleans, you can do it like this.

Let's say you assign that object to a variable.

var myObject = {  
    "User1": {  
        "age":15,
        "country":"sw",
        "glasses":true,
        "car":true,
        "zip":1223,
        "name":"Frank"
    },
    "User2": {  
        "age":23,
        "country":"fr",
        "glasses":false,
        "car":true,
        "zip":5577,
        "name":"Mike"
    }
}

Then you can loop through the object and assign the filtered results to myBooleans.

var myBooleans = {};

Object.keys(myObject).forEach(function(key) {
    var myNewUserObject = {};
    Object.keys(myObject[key]).forEach(function(userKey) {
        if (typeof myObject[key][userKey] === 'boolean') {
            myNewUserObject[userKey] = myObject[key][userKey];
        }
    });
    myBooleans[key] = myNewUserObject;
});

There is probably a pretty way to do with with n levels of object using recursion. But this will work for the shape of the data you provided.

Comments

0

Try this

function parse(obj , targetObject) {
var k;
if (obj instanceof Object) {
    for (k in obj){
        if(typeof(obj[k]) === "boolean"){
            targetObject[k] = obj[k];
        }
        if(obj[k] instanceof Object) {
            targetObject[k] = {};
            parse(obj[k],targetObject[k]);
        }
    }
} 

};

Call like this:

var target = {};
parse(myObj , target);

Hope this helps

Comments

0

This is how you can filter all the boolean values from object -

let input = {
  "User1": {
    "age": 15,
    "country": "sw",
    "glasses": true,
    "car": true,
    "zip": 1223,
    "name": "Frank"
  },
  "User2": {
    "age": 23,
    "country": "fr",
    "glasses": false,
    "car": true,
    "zip": 5577,
    "name": "Mike"
  }
};

let output = {};
Object.entries(input).map(([key, value], index) => {
  output[key] = {};
  Object.entries(value).
      filter(([k, v], i) => typeof v === "boolean").
      map(([k, v], i) => {
        output[key][k] = v;
      })
});

console.log(output);

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.