5 回答
TA贡献1810条经验 获得超5个赞
let state={
new1:1,
new2:2,
new3:3,
old1:1,
old2:2
};
const BLACK_LIST = ["old1", "old2"];
for(let p in state) {
if(BLACK_LIST.indexOf(p) == -1)
console.log(p, state[p])
}
TA贡献1818条经验 获得超3个赞
对象用for in循环
const state = {
news1: 1,
news2: 2,
news3: 3,
old: 1,
old2: 2
}
var arr = []
for (let o in state) {
if (o.indexOf('news') > -1) {
arr.push({[o]: state[o]})
}
}
console.log(arr)
TA贡献1812条经验 获得超5个赞
可以把不想枚举的属性的enumerable设置为false,这样for in的话就不会循环出来了:
function notEnum (obj, keys) {
keys.forEach(key => {
let def = Object.getOwnPropertyDescriptor(obj, key)
def.enumerable = false
Object.defineProperty(obj, key, def)
})
}
var state = {
new1:1,
new2:2,
new3:3,
old1:1,
old2:2
}
notEnum(state, ['old1', 'old2']) // 把old1和old2设置为不可枚举属性
for (let i in state) {
console.log(i)
}
添加回答
举报