1. /**
    2. * [过滤对象]
    3. * @param obj [过滤前数据]
    4. * @param arr [过滤条件,要求为数组]
    5. */
    6. function filterObj(obj, arr) {
    7. if (typeof (obj) !== "object" || !Array.isArray(arr)) {
    8. throw new Error("参数格式不正确")
    9. }
    10. const result = {}
    11. Object.keys(obj).filter((key) => arr.includes(key)).forEach((key) => {
    12. result[key] = obj[key]
    13. })
    14. return result
    15. }
    16. /**
    17. 使用
    18. **/
    19. let obj = {
    20. a: '1',
    21. b: '2',
    22. c: '3'
    23. }
    24. let newObj = filterObj(obj,["a", "b"]);
    25. 返回结果 :
    26. newObj{
    27. a: '1',
    28. b: '2'
    29. }

    原文链接:https://blog.csdn.net/qq_33401924/article/details/88398449