减少对象思想object.entries

时间:2018-04-20 15:37:03

标签: javascript ecmascript-6

我有这种对象

我希望使用“存在=== 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);

1 个答案:

答案 0 :(得分:4)

您可以通过以下代码

来达到所需的结果

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;}