我有一个对象'ecom',它将具有['detail','add','remove','checkout','purchase']
我想知道对象具有5个潜在属性中的哪个。
最简单,最干净的方法是什么?
答案 0 :(得分:5)
您可以使用filter()
和hasOwnProperty()
let arr = ['detail','add','remove','checkout','purchase'];
let obj = {detail:'val',add:0,purchase:33}
let res = arr.filter(x => obj.hasOwnProperty(x));
console.log(res)
let arr = ['detail','add','remove','checkout','purchase'];
let obj = {detail:'val',add:0,purchase:33}
let res = arr.filter(function(x){
return obj.hasOwnProperty(x)
})
console.log(res)