简体   繁体   English

Vue/JS - 检查两个对象的所有值是否为空

[英]Vue/JS - Check if all values of two objects are null

I have two objects with all the properties set to null.我有两个对象,所有属性都设置为 null。 I want to execute a computed function to check whether all the values in both these objects are null.我想执行一个计算函数来检查这两个对象中的所有值是否都为空。 For one specific object I created the following: Object.values(endField.value).every(x => x === null)对于一个特定对象,我创建了以下内容: Object.values(endField.value).every(x => x === null)

How can I do the same thing for two objects without being too repetitive in my code?我怎样才能对两个对象做同样的事情而不会在我的代码中过于重复?

const startField = ref({ date: null, month: null, year: null })
const endField = ref({ date: null, month: null, year: null })

// If the properties of both objects are all null, isEmpty should be true.
const isEmpty = computed(() => {
    return Object.values(endField.value).every(x => x === null);
});

This is working for me:这对我有用:

If any property has a value, returns false, otherwise true.如果任何属性有值,则返回 false,否则返回 true。

        const startField = ref({ date: null, month: "Aug", year: null });
        const endField = ref({ date: null, month: null, year: null });

        const isEmpty = computed(() => {
            return (
                Object.values(endField.value).every((x) => x === null) ||
                Object.values(startField.value).every((x) => x === null)
            );
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM