我有一个对象“ ecom”,它将具有以下属性之一 ['detail','add','remove','checkout','purchase']
我想知道对象具有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)Run Code Online (Sandbox Code Playgroud)
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)Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
39 次 |
| 最近记录: |