-
森栏
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])}
-
紫衣仙女
for(var value in state){ console.log(state[value])}
-
泛舟湖上清波郎朗
对象用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)
-
慕雪6442864
可以把不想枚举的属性的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)}