Lodash如果我没有要排除的属性,如何验证属性是否在数组中相等

时间:2019-11-27 02:59:18

标签: javascript arrays lodash

假设我有这样的员工数据

var service = [{
        AssignedEmployeeInternalID: "8000000011",
        AssignedEmployeeUUID: "00000000-0001-1EE1-82C1-8379F1C4462E",
        BillableIndicator: true,
        BusinessPartnerFormattedName: "Sébastien Brun",
        EndDateTime: null,
        EndDatetimeZoneCode: "UTC",
        Info: "-N/A",
        InternalID: "",
        ObjectID: "00163E0E46241EE988E20C5E4033E0BE",
        ParentObjectID: "00163E0306801ED287F1C8E9CDFA9E4A",
        PeriodPlansArray: [],
        PlannedWorkQuantity: "0.00000000000000",
        PlannedWorkQuantityunitCode: "HUR",
        PlannedWorkQuantityunitCodeText: "",

    },
    {
        AssignedEmployeeInternalID: "8000000011",
        AssignedEmployeeUUID: "00000000-0001-1EE1-82C1-8379F1C4462E",
        BillableIndicator: false,
        BusinessPartnerFormattedName: "Sébastien Brun",
        EndDateTime: null,
        EndDatetimeZoneCode: "UTC",
        Info: "-N/A",
        InternalID: "",
        ObjectID: "00163E0E46241EE988E20C5E4033E0BE",
        ParentObjectID: "00163E0306801ED287F1C8E9CDFA9E4A",
        PeriodPlansArray: [],
        PlannedWorkQuantity: "0.00000000000000",
        PlannedWorkQuantityunitCode: "HUR",
        PlannedWorkQuantityunitCodeText: "",

    },
    {
        AssignedEmployeeInternalID: "8000000011",
        AssignedEmployeeUUID: "00000000-0001-1EE1-82C1-8379F1C4462E",
        BillableIndicator: true,
        BusinessPartnerFormattedName: "Sébastien Brun",
        EndDateTime: null,
        EndDatetimeZoneCode: "UTC",
        Info: "-N/A",
        InternalID: "",
        ObjectID: "00163E0E46241EE988E20C5E4033E0BE",
        ParentObjectID: "00163E0306801ED287F1C8E9CDFA9E4A",
        PeriodPlansArray: [],
        PlannedWorkQuantity: "0.00000000000000",
        PlannedWorkQuantityunitCode: "HUR",
        PlannedWorkQuantityunitCodeText: "",
    }
]

如何验证AssignedEmployeeInternalID在所有数组中是否相同?

到目前为止,我想到了这个:

result =  _.differenceBy(service, [{ 'AssignedEmployeeInternalID': "8000000011" }], 'AssignedEmployeeInternalID'); 

但是如果我没有要排除的值(我事先不知道AssignedEmployeeInternalID值)怎么办

我要动态验证属性是否相同

2 个答案:

答案 0 :(得分:1)

如果您按如下所示使用Array every方法,则可以在普通JavaScript中完成此操作:

var service = [{
        AssignedEmployeeInternalID: "8000000011",
        AssignedEmployeeUUID: "00000000-0001-1EE1-82C1-8379F1C4462E",
        BillableIndicator: true,
        BusinessPartnerFormattedName: "Sébastien Brun",
        EndDateTime: null,
        EndDatetimeZoneCode: "UTC",
        Info: "-N/A",
        InternalID: "",
        ObjectID: "00163E0E46241EE988E20C5E4033E0BE",
        ParentObjectID: "00163E0306801ED287F1C8E9CDFA9E4A",
        PeriodPlansArray: [],
        PlannedWorkQuantity: "0.00000000000000",
        PlannedWorkQuantityunitCode: "HUR",
        PlannedWorkQuantityunitCodeText: "",

    },
    {
        AssignedEmployeeInternalID: "8000000011",
        AssignedEmployeeUUID: "00000000-0001-1EE1-82C1-8379F1C4462E",
        BillableIndicator: false,
        BusinessPartnerFormattedName: "Sébastien Brun",
        EndDateTime: null,
        EndDatetimeZoneCode: "UTC",
        Info: "-N/A",
        InternalID: "",
        ObjectID: "00163E0E46241EE988E20C5E4033E0BE",
        ParentObjectID: "00163E0306801ED287F1C8E9CDFA9E4A",
        PeriodPlansArray: [],
        PlannedWorkQuantity: "0.00000000000000",
        PlannedWorkQuantityunitCode: "HUR",
        PlannedWorkQuantityunitCodeText: "",

    },
    {
        AssignedEmployeeInternalID: "8000000011",
        AssignedEmployeeUUID: "00000000-0001-1EE1-82C1-8379F1C4462E",
        BillableIndicator: true,
        BusinessPartnerFormattedName: "Sébastien Brun",
        EndDateTime: null,
        EndDatetimeZoneCode: "UTC",
        Info: "-N/A",
        InternalID: "",
        ObjectID: "00163E0E46241EE988E20C5E4033E0BE",
        ParentObjectID: "00163E0306801ED287F1C8E9CDFA9E4A",
        PeriodPlansArray: [],
        PlannedWorkQuantity: "0.00000000000000",
        PlannedWorkQuantityunitCode: "HUR",
        PlannedWorkQuantityunitCodeText: "",
    }
];

const theSame = service.every(el => el.AssignedEmployeeInternalID === service[0].AssignedEmployeeInternalID);

console.log(theSame);

答案 1 :(得分:1)

您可以使用uniqBy(请参阅文档https://lodash.com/docs/4.17.15#uniqBy),然后如果结果数组的length1,则所有数组项的{{ 1}}:

AssignedEmployeeInternalID