最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Check if the selected objects have field with Similar or Different values? - Stack Overflow

programmeradmin0浏览0评论

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 -

  1. Objects are having same values
  2. 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 -

  1. Objects are having same values
  2. 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
  • What value are you passing to affadavidCurrentValuesEvaluator()? – mykaf Commented Feb 6 at 17:09
  • 3 Can you update this to a minimal reproducible example which can be independently executed to observe the problem you're trying to describe? An example should include the data being used, the operation(s) being performed, and the result being observed. This is also a good time for you to step through your code in a debugger and identify the exact operation which isn't doing what you expect. – David Commented Feb 6 at 17:10
  • 1 Why do you take an argument called selectedRecords to valuesEvaluator() and then immediately overwrite it? – mykaf Commented Feb 6 at 17:41
  • 1 Have you checked your dev tools, as David mentioned? This code doesn't run, let alone give any values. – mykaf Commented Feb 6 at 18:17
  • 1 @Bhupesh: The code shown produces no result because all it does is define two functions but never calls them. If you add valuesEvaluator() 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
 |  Show 2 more comments

3 Answers 3

Reset to default 1

The 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();

发布评论

评论列表(0)

  1. 暂无评论