Untitled

Anonymous
plain_text
03/05/2022 6:54 AM
1.0 KB
17
Indexable
let allowedFields=["firstName", "email", "_id"];
let arrOfUsers=[
    {
        _id:"abc",
        firstName:"Shekhar",
        lastName:"Kadam",
        email:"[email protected]",
        createdAt:"67"
    },
    {
        _id:"def",
        firstName:"Harsh",
        lastName:"Singh",
        email:"[email protected]",
        createdAt:"67",
        npo:90
    },
    {
        _id:"ghi",
        firstName:"sushant",
        lastName:"singh",
        email:"[email protected]"
    },
]
let newArray=[];
arrOfUsers.map((eachUser)=>{
    const userKeys=Object.keys(eachUser);
    let newObj={};
    userKeys.filter((eachKey)=>allowedFields.includes(eachKey)).map((newKey)=>{
        newObj[newKey]=eachUser[newKey];
    });
    newArray.push(newObj);
});


console.log(newArray)
Editor is loading...