I want code optimisation for an if
-statement because I otherwise have to add more key-value pairs into this condition with &&
. I have an array of objects and the condition as with the following code example.
let arr = [{
a: 12,
b: 14,
c: undefined,
d: undefined,
e: 56,
f: "file 1",
g: "file 2",
h: undefined,
}];
for (const item of arr) {
if (
item.a !== undefined &&
item.b !== undefined &&
item.c !== undefined &&
item.d !== undefined &&
item.e !== undefined &&
item.f !== undefined &&
item.g !== undefined
) {
console.log("code works");
} else {
console.log("fails");
}
}
I'm trying to optimise the above code, and I appreciative any suggestion.
CodePudding user response:
I understand the Q. in a way that the OP does not want to check for every object's value being not the undefined
value, and the OP rather wants to check whether some predefined properties (e.g. by a key list) need to be present and also not equal to undefined
.
In this case one could implement a function which checks via a list of property names (key list) for each passed item/object whether such an object fulfills the above mentioned requirement.
Such a function could be passed as callback function to every
in order to detect whether any of an array's item/object does fulfill the requirement.
function isEveryPropertyFromBoundListDefined(item) {
const keyList = this; // the bound list of defined keys.
return keyList
.every(key => item[key] !== undefined);
}
const sampleData_1 = [{
a: 12, b: 14, c: undefined, d: undefined,
e: 56, f: "file 1", g: "file 2", h: undefined,
}];
const sampleData_2 = [{
e: 56, f: "file 1", g: "file 2", h: null,
}, {
h: 1, g: 2, f: 3, e: 4,
}];
const listOfToBeDefinedKeys = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
console.log(
sampleData_1
// make use of `every`'s 2nd `thisArg` parameter.
.every(isEveryPropertyFromBoundListDefined, listOfToBeDefinedKeys)
);
console.log(
sampleData_2
// make use of `every`'s 2nd `thisArg` parameter.
.every(isEveryPropertyFromBoundListDefined, ['e', 'f', 'g', 'h'])
);
.as-console-wrapper { min-height: 100%!important; top: 0; }
CodePudding user response:
You can use Object.values()
and some()
to write a shorter code. This is not a huge optimization
let arr = [{
a:12,
b:14,
c:undefined,
d:undefined,
e:56,
f:"file 1",
g:"file 2",
h:undefined
}]
for(let key of arr){
if(Object.values(key).some((x) => x===undefined)){
console.log("fails")
}else{
console.log("code works")
}
}
CodePudding user response:
Create a function that accepts an object as an argument, gets its values
, and then use some
to check if any of the values are undefined
. some
returns after finding the first match to the condition.
const arr=[{a:12,b:14,c:undefined,d:undefined,e:56,f:'file 1',g:'file 2',h:undefined},{a:12,b:14,c:3,d:'bob from accounting',e:56,f:'file 1',g:'file 2',h:23},{a:12,b:14,c:3,d:'bob from accounting',e:56,f:'file 1',g:'file 2',h:undefined}];
function test(obj) {
const values = Object.values(obj);
return values.some(p => p === undefined);
}
const out = arr.map((obj, i) => {
return `Code ${test(arr[i]) ? 'fails' : 'works'}`;
});
console.log(out);
CodePudding user response:
If I understand the requirement correctly, You have dynamic properties in an object and you want to check if all the properties in an object having values. If Yes,
Try this :
let arr = [{
a:12,
b:14,
c:undefined,
d:undefined,
e:56,
f:"file 1",
g:"file 2",
h:undefined
}];
arr.forEach(obj => {
let str = '';
Object.keys(obj).forEach(key => {
str = obj[key] ? 'code works' : 'fails'
})
console.log(str);
});