I have to project some fields of javascript to new object.
for example I have a below object
var obj = { fn : 'Abc',
ln : 'Xyz',
id : 123,
nt : 'Note',
sl : 50000}
and i want new object containing fn and id
var projectedObj = { fn : 'Abc', id : 123 }
on the basis of projection
var projection = { fn : 1, id : 1 }
something like this
var projectedObj = project(obj, projection);
So what is the best way or optimized way to do this.
Just loop through the projection object and get the keys projected. For example,
function project(obj, projection) {
let projectedObj = {}
for(let key in projection) {
projectedObj[key] = obj[key];
}
return projectedObj;
}