How to dynamically Filter a specified value from an object in js

implements a method that dynamically Filter outputs the value of the specified key in the object arr and returns a new object;

/ / Raw data

  let arr= {
        a:"",
        b:"",
        c:"",
    }
   

/ / execute the method fun (["a", "b"]) to get the object after Filter

  let arr= {
        a:"",
        b:""
    }
Oct.13,2021

let arr= {
        a:"",
        b:"",
        c:"",
    }
var obj = {};

function fun(arg){
    for(var i in arr){
        for(var j = 0; j<arg.length;jPP){
            if(i==arg[j]){
                obj[i] = arr[i]
            }
        }
    }

}


solve

< hr >
ignore the way attributes are inherited
const result = [];
for (let key in arr) {
  if (['a', 'b'].includes(key)) {
    result.push(arr[key]);
  }
}

reference

< hr >

I happen to see one in the project tool class. no, no, no. I'll give it to you

.
function getTargetObject(targetObject, propsArray) {
    if (typeof (targetObject) !== "object" || !Array.isArray(propsArray)) {
        throw new Error("");
    }
    const result = {};
    Object.keys(targetObject).filter(key => propsArray.includes(key)).forEach(key => {
        result[key] = targetObject[key];
    })
    return result;
}

the following is the effect of use. no, no, no.

clipboard.png


Object.keys(JSON.parse(JSON.stringify({a:"",b:"",c:""},['a','b'])))

give me a hint

get the object key name
Object.keys ()


var b = {

    a:"",
    b:"",
    c:"",
}

function fun (arr,obj) {
let o = {}
Object.keys (obj) .forEach (item= > {
if (~ arr.indexOf (item)) {
o [item] = obj [item]
}
})
return o
}
var obj = fun


lodash.pick

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b35bc8-2ad00.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b35bc8-2ad00.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?