-6

I have a JSON response with the key and the value, I want to filter only those whose weight is 1 Any help is greatly appreciated

{
"post": 1,
"bio": 1,
"hashtag": 0,
"profile": 0,
}

expected output

{
"post": 1,
"bio": 1
}
  • Parse the JSON to a JavaScript object (`JSON.parse`), convert the object to an array (`Object.entries`), filter the array (`Array.prototype.filter`), convert the array to an object (`Object.fromEntries`) and serialize the object (`JSON.stringify`). – jabaa Sep 29 '22 at 13:08
  • 1
    Does this answer your question? [How to filter an object with its values in ES6](https://stackoverflow.com/questions/44025984/how-to-filter-an-object-with-its-values-in-es6) – paulogdm Sep 29 '22 at 13:09
  • 1
    someone close this Q ... – KcH Sep 29 '22 at 13:44

1 Answers1

-2

You can filter over Object.entries.

const o = {
  "post": 1,
  "bio": 1,
  "hashtag": 0,
  "profile": 0,
};
let res = Object.fromEntries(Object.entries(o).filter(([k,v])=>v===1));
console.log(res);
Unmitigated
  • 76,500
  • 11
  • 62
  • 80