If I have something like,
const {
someProperty,
anotherProperty,
thirdProperty
} = someObject;
const includeProperties = ['someProperty', 'thirdProperty']
Is it possible to make anotherProperty = null?
You may nullify all the not needed properties within source object shallow copy and destructure that:
const srcObj = {a:1, b:2, c:3},
neededKeys = ['a','b'],
objCopy = (obj =>
(Object.keys(obj).forEach(key =>
!neededKeys.includes(key) && (obj[key]=null)), obj)
)({...srcObj}),
{a,b,c} = objCopy
console.log(a,b,c)
.as-console-wrapper{min-height:100%;}
c in obj would exist in the case of the question.You can use Array.prototype.reduce
let properties = [
'someProperty',
'anotherProperty',
'thirdProperty'
]
const includeProperties = ['someProperty', 'thirdProperty'];
let object = properties.reduce((acc, val) => {
if (includeProperties.indexOf(val) === -1)
acc[val] = null;
else
acc[val] = 'some value';
return acc;
}, {});
console.log(object);
let someProperty = someObject,
anotherProperty = someObject,
thirdProperty = someObject;
const includeProperties = [someProperty, thirdProperty];
if (!includeProperties.include(someVariable))
someVariable= null;
}
This is a possible way to do what you asked.
includeProperties