I am trying to write logic that compares a set of selected Objects. I need to either print the value if it is similar across all objects or print various
if the value is different for a particular field.
I am able to print correct values when -
- Objects are having same values
- If the 1st selected object does not have any empty value, then for any empty value in the second object - I am getting
various
text which is also correct.
Getting incorrect value for -
If the 1st selected object is having any null/undefined
value and for the 2nd object, there is no null/undefined
then I am getting the second object - It should print various
Remember - There can be n
number of selected objects.
Code -
const determineUniformity = (fieldValue, recordValue) => {
if (fieldValue) {
if (fieldValue === recordValue) {
return fieldValue;
} else {
return 'various';
}
} else {
return recordValue;
}
};
const valuesEvaluator = selectedRecords => {
//just for explaining overwriting
selectedRecords = [
{ name: 'record1', height: null, width: 'width1' },
{ name: 'record2', height: 'height2', width: null }
];
let name, height, width;
selectedRecords.forEach(record => {
name = determineUniformity(name, record.name);
height = determineUniformity(height, record.height);
width = determineUniformity(width, record.width);
});
console.log('name', name); //Output - Various --> Correct
console.log('height', height); //Output - Height2 --> Incorrect
console.log('width', width); //Output - Various --> Correct
};
valuesEvaluator();
I am trying to write logic that compares a set of selected Objects. I need to either print the value if it is similar across all objects or print various
if the value is different for a particular field.
I am able to print correct values when -
- Objects are having same values
- If the 1st selected object does not have any empty value, then for any empty value in the second object - I am getting
various
text which is also correct.
Getting incorrect value for -
If the 1st selected object is having any null/undefined
value and for the 2nd object, there is no null/undefined
then I am getting the second object - It should print various
Remember - There can be n
number of selected objects.
Code -
const determineUniformity = (fieldValue, recordValue) => {
if (fieldValue) {
if (fieldValue === recordValue) {
return fieldValue;
} else {
return 'various';
}
} else {
return recordValue;
}
};
const valuesEvaluator = selectedRecords => {
//just for explaining overwriting
selectedRecords = [
{ name: 'record1', height: null, width: 'width1' },
{ name: 'record2', height: 'height2', width: null }
];
let name, height, width;
selectedRecords.forEach(record => {
name = determineUniformity(name, record.name);
height = determineUniformity(height, record.height);
width = determineUniformity(width, record.width);
});
console.log('name', name); //Output - Various --> Correct
console.log('height', height); //Output - Height2 --> Incorrect
console.log('width', width); //Output - Various --> Correct
};
valuesEvaluator();
Share
Improve this question
edited Feb 7 at 7:07
DarkBee
15.6k8 gold badges70 silver badges115 bronze badges
asked Feb 6 at 17:08
BhupeshBhupesh
458 bronze badges
7
|
Show 2 more comments
3 Answers
Reset to default 1The problem is that if (fieldValue)
will succeed either if the field value variable hasn't been initialized or if the value from the first row is null
, 0, an empty string, or any other falsey value. To distinguish these cases, check explicitly for undefined
, which is the default value for variables that are declared but not initialized.
Another problem is selectedRecords.value.forEach
. selectedRecords
is an array, it has no value
property. Just use selectedRecords.forEach
.
const determineUniformity = (fieldValue, recordValue) => {
if (fieldValue !== undefined) {
if (fieldValue === recordValue) {
return fieldValue;
} else {
return 'various';
}
} else {
return recordValue;
}
};
const valuesEvaluator = selectedRecords => {
//just for explaining overwriting
selectedRecords = [
{ name: 'record1', height: null, width: 'width1' },
{ name: 'record2', height: 'height2', width: null }
];
let name, height, width;
selectedRecords.forEach(record => {
name = determineUniformity(name, record.name);
height = determineUniformity(height, record.height);
width = determineUniformity(width, record.width);
});
console.log('name', name); //Output - Various --> Correct
console.log('height', height); //Output - Height2 --> Incorrect
console.log('width', width); //Output - Various --> Correct
};
valuesEvaluator();
Looking at the OP's code, there seems to be an issue accessing just the values of each array element. Using Object.values(array), one can get just the array of values of an enumerable string-keyed property values.
const determineUniformity = (fieldValue, recordValue) => {
if (fieldValue) {
if (fieldValue === recordValue) {
return fieldValue;
} else {
return 'various';
}
} else {
return recordValue;
}
};
const valuesEvaluator = (selectedRecords) => {
let name, height, width ;
Object.values(selectedRecords).forEach(record => {
name = determineUniformity(name, record.name);
height = determineUniformity(height, record.height);
width = determineUniformity(width, record.width);
console.log('name: ' + name);
console.log('height: ' + height);
console.log('width: ' + width);
});
};
let SelectedObject = [
{ "name": "record1", "height": null, "width": "width1"},
{ "name": "record2", "height": "height2", "width": "width2" }
];
valuesEvaluator(SelectedObject);
Found another solution to compare a single Property inside multiple objects.
const compareObjects = (values, field) => {
const firstValue = values[0][field];
let allSame = true;
for (let i = 1; i < values.length; i++) {
const currentValue = values[i][field];
if (firstValue !== currentValue) {
allSame = false;
break;
}
}
return allSame ? firstValue : 'various';
};
const valuesEvaluator = selectedRecords => {
//just for explaining overwriting
selectedRecords = [
{ name: 'record1', height: null, width: 'width1' },
{ name: 'record2', height: 'height2', width: null }
];
let name, height, width;
name = compareObjects(selectedRecords, 'name');
height = compareObjects(selectedRecords, 'height');
width = compareObjects(selectedRecords, 'width');
console.log('name', name); //Output - Various --> Correct
console.log('height', height); //Output - Various --> Correct
console.log('width', width); //Output - Various --> Correct
};
valuesEvaluator();
affadavidCurrentValuesEvaluator()
? – mykaf Commented Feb 6 at 17:09selectedRecords
tovaluesEvaluator()
and then immediately overwrite it? – mykaf Commented Feb 6 at 17:41valuesEvaluator()
at the end to call that function, an error is produced. This code does not demonstrate the problem you're trying to describe. Please refer to a previous comment. – David Commented Feb 6 at 19:16