/**
* [过滤对象]
* @param obj [过滤前数据]
* @param arr [过滤条件,要求为数组]
*/
function filterObj(obj, arr) {
if (typeof (obj) !== "object" || !Array.isArray(arr)) {
throw new Error("参数格式不正确");
}
const result = {};
Object.keys(obj).filter((key) => arr.includes(key)).forEach((key) => {
result[key] = obj[key];
})
return result;
}
/**
使用
**/
let obj = {
a: '111',
b: '222',
c: '333'
}
let resultObj = filterObj(obj,["a", "b"]);
console.log('resultObj======', resultObj);
返回结果 :
{
a: '1111',
b: '222'
}
image.png