node.js - How to find common properties between JavaScript objects -


What is the best / most effective way to find common / different properties of an array of objects

I need to identify the set of properties present in all the objects and all have the same value (normal). Preferably, I would like to get an array with all other properties (difference)

I have discovered a skilled library / function which can do this. But nothing was found. 'Bob'}, {/ id} id: '2j2w3f', color: 'red', height: 20, width: 41, owner: 'Bob'}, {id: '2j2w2f', color: 'red', height: 21,}, {id: 2j2w1f ', color:' red ', height: 21, width: 44}];

I would like to identify the color (value red ) as the only general property, note that they have the same properties Not set as owner is not a common property.

This is my own attempt to solve (using the trash):

  function General Defofriftes (objects) {// Normal start as initial object, and Then remove non-normal properties var normal = objects [0]; Var different = []; Var i, obj; // Again through the rest (note: I === 0 is not considered cover for this) (i = objects. Lamps - 1; i> I;) {obj = objects [i]; // Compare every property in OBIZ to the current general _. If the property is not in the current general then it should be different (_.isUndefined (common [key]) {if (! _ (Different, key) {Different.push (key);}} and if (general [key]! == value) {// Removing the property from mango; if the value is not equal then common [key]; different key (key);} }; // Check that all properties of mangoes are present in obj, if not, then remove from normal. _.forOwn (normal, function (value, key) {if (_.isUndefined (obj [key])) { Common Withdrawal); separate; push (key);}});} return {general: common, different: different};}   

I also I tried to improve the map, but it seemed worse than this.

I still think that it takes a bit complicated / time consuming, and I call it 1,000-10000 objects or more 20-50

Any suggestions?

There are two things that your solution Inaccurate:

  • Objects <0>; You Do not copy the code, so you have corrupted objects
  • both of you check that all properties of mangoes are present in obj , but Compare every property of obj with current general It seems that once more is too big. For the first time you did not understand that you also needed the different properties.

    Data in two passes first, you collect all the obvious properties in one object, in the second you check whether they are normal or not:

     < code> Function NormalDiffyProperties (objects) {var common = _.reduce (objects), function (ACC, OBJ) {for obj in var p) ACC [P] = obj [P]; Return ac;}, {}); var different = _.reduce {for (ac, obez) (for general pi) if (normal [p]! == oz [p]) {common deletion [p]; apesp (p); } Return ACC;}, []); Return {general: common, different: different}; }    

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -