减少对象思想object.entries

use*_*714 1 javascript ecmascript-6

我有这种对象

我想使用具有“ 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);
Run Code Online (Sandbox Code Playgroud)

Nar*_*hav 5

您可以通过以下代码获得所需的结果

演示

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);
Run Code Online (Sandbox Code Playgroud)
.as-console-wrapper {  max-height: 100% !important;  top: 0;}
Run Code Online (Sandbox Code Playgroud)