简体   繁体   English

比较两个对象中选定属性的最佳方法是什么

[英]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.

相关问题 从两个单独的JavaScript对象合并属性的最佳方法是什么? - What is the best way to merge properties from two separate JavaScript objects? 比较两个街道地址的最佳方法是什么? - What's the best way to compare two street addresses? 比较两个对象的值的最有效方法是什么? - What's the most efficient way to compare values of two objects? 查看数组是否具有两个对象的两个不同属性的值相同的最佳方法是什么? - What is the best way to see if an array has two objects with the same values for two distinct properties? 比较字符串数组和具有字符串属性的数组对象的最佳方法? - Best way to compare an array of strings to an array objects with string properties? 比较JS中两个对象的属性 - Compare two objects properties in JS 比较 Google 表格中两列数据和 output 只有使用 Google 脚本的不匹配数据的最佳方法是什么? - What's the best way to compare two columns of data in Google Sheets and output ONLY the non-matching using Google Scripts? 在JavaScript中不变地更新对象某些属性的最佳方法是什么? - What's the best way to update some properties of an object immutably in JavaScript? 在Ember数据中,在两个对象之间形成关联的最佳方法是什么? - In Ember data, what is the best way to form association between two objects? 在这种情况下比较两个对象布尔的更好方法是什么? - What's a better way to compare two object booleans in this scenario?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM