[英]what's the best way to compare selected properties in two objects
I need to compare properties in two objects.我需要比较两个对象的属性。
object2
is an array of objects and each item in the array has a lot more 'properties' than object1
so I can't do a direct object to object comparison. object2
是一个对象数组,数组中的每个项目都比object1
具有更多的“属性”,因此我无法直接进行对象到对象的比较。 I only want to compare the properties that exist in object1
with those same properties in object2
(and ignore properties that don't exist in object1
).我只想将
object1
中存在的属性与object2
相同属性进行比较(并忽略object1
不存在的属性)。
The following works but seems very cumbersome.以下工作,但似乎很麻烦。 (Added a sample data. Ignore that you can't
itemInArray.prop1 === ""
on a number :-) (添加了示例数据。忽略您不能在数字上使用
itemInArray.prop1 === ""
:-)
var object1 =
{
prop1: "min",
prop2: 134,
prop3: 121,
prop4: 3314,
prop5: 0
}
var object2 = [
{
prop1: "min",
prop2: 237,
prop3: 563,
prop4: 23,
prop5: 5,
prop6: "mrt",
prop7: "New York",
prop8: "usa"
},
{
prop1: "min",
prop2: 134,
prop3: 121,
prop4: 3314,
prop5: 0,
prop6: "mrt",
prop7: "london",
prop8: "uk"
},
{
prop1: "min",
prop2: 1,
prop3: 12,
prop4: 14,
prop5: 2,
prop6: "mrt",
prop7: "Berlin",
prop8: "Germany"
}
];
var result = object2.filter(
(itemInArray) =>
object1.prop0 === itemInArray.prop0 &&
(object1.prop1 >= itemInArray.prop1 || itemInArray.prop1 === "") &&
(object1.prop2 <= itemInArray.prop2 || itemInArray.prop2 === "") &&
(object1.prop3 >= itemInArray.prop3 || itemInArray.prop3 === "") &&
(object1.prop4 <= itemInArray.prop4 || itemInArray.prop4 === "") &&
(object1.prop5 <= itemInArray.prop5 || itemInArray.prop5 === "")
)
You could implement it with something like the following:您可以使用以下内容来实现它:
// defining initial compare rules for numbers. // You might want to modify them depending on the possible content. const eq = (a, b) => a === b; const lte = (a, b) => a <= b; const lt = (a, b) => a < b; const gte = (a, b) => a >= b; const gt = (a, b) => a > b; const obj1 = { prop1: 1, prop2: 2 }; const obj2 = { prop1: 1, prop2: 1 }; const keyRulesPairs = { prop1: eq, // specifying the compare rule for the certain keyName prop2: gt }; function compareObjects(obj1, obj2, keyRulesPairs) { return Object.keys(keyRulesPairs).every(key => { // every for all the `&&`s. const compare = keyRulesPairs[key]; return compare(obj1[key], obj2[key]); }) } console.log(compareObjects(obj1, obj2, keyRulesPairs));
As for getting only those objects from the array which are true
by your compare rules, you just need to call with filter
method, like this:至于仅从数组中获取那些根据比较规则为
true
对象,您只需要使用filter
方法调用,如下所示:
const listOfObjects = [
{ prop1: 1, prop2: 1 },
{ prop1: 1, prop2: 3 }
];
const filteredObjects = listOfObjects.filter(obj2 => {
return compareObjects(obj1, obj2, keyRulesPairs);
})
console.log(filteredObjects)
I'd recommend using property accessors .我建议使用属性访问器。 This way you can make your code quite generic.
通过这种方式,您可以使您的代码非常通用。
View the next code sample.查看下一个代码示例。 I will be using TypeScript so it becomes clear what types everything can be.
我将使用 TypeScript,所以一切都可以是什么类型变得很清楚。
In a normal case:在正常情况下:
const compare = (obj1: object, obj2: object, propertyToCompare: string): boolean => {
return obj1['propertyToCompare'] === obj2['propertyToCompare'];
}
You can easily loop through all the properties of an object using Object.keys()
您可以使用
Object.keys()
轻松遍历对象的所有属性
Object.keys(obj1).forEach(prop => {
console.log(compare(obj1, obj2, prop));
});
Ofcourse in your example, you would also loop through all the objects of your array before doing the comparison.当然,在您的示例中,您还可以在进行比较之前循环遍历数组的所有对象。 As such:
像这样:
obj2.forEach(obj => { // Going through all the objects from the array of objects (obj2)
Object.keys(obj1).forEach(prop => { // Only using the properties from obj1
console.log(compare(obj1, obj, prop)); // Comparing obj (object in array) to obj1
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.