Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reduce object thought object.entries

I have this kind of object

I want to get new object with keys that have "exist === true"

const someObj  = {
      super: {
        exist: true
      },
      photo: {
        exist: true
      },
      request: {
        exist: false
      }
}
const newObj = Object.entries(someObj).reduce((newObj, [key, val]) => {
  if (this.key.exist) { // how to check "exist" is true ?
    return { ...newObj, [key]: val }
  }
}, {});

console.log(newObj);
like image 681
user1898714 Avatar asked Apr 20 '18 15:04

user1898714


1 Answers

You can be achieve your required result by following code

DEMO

const someObj = {
  super: {
    exist: true
  },
  photo: {
    exist: true
  },
  request: {
    exist: false
  }
};
const newObj = Object.entries(someObj).reduce((newObj, [key, val]) => {
  if (val.exist) {
    newObj[key] = val;
  }
  return newObj;
}, {})

console.log(newObj);
.as-console-wrapper {  max-height: 100% !important;  top: 0;}
like image 112
Narendra Jadhav Avatar answered Oct 17 '22 13:10

Narendra Jadhav