Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
784 B
2
Indexable
Never
let allowedFields=["firstName", "email", "_id"];
let arrOfUsers=[
    {
        _id:"abc",
        firstName:"Shekhar",
        lastName:"Kadam",
        email:"shekharkadam@dhwani.com",
        createdAt:"67"
    },
    {
        _id:"def",
        firstName:"Harsh",
        lastName:"Singh",
        email:"hvs@dhwani.com",
        createdAt:"67",
        npo:90
    },
    {
        _id:"ghi",
        firstName:"sushant",
        lastName:"singh",
        email:"sush@dhwani.com"
    },
]
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)