我有这种对象
我希望使用“存在=== 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);
答案 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;}