简体   繁体   English

使用node.js比较两个对象

[英]compare two objects using node.js

Am trying to compare two objects using node.js . 我正在尝试使用node.js比较两个对象。 ie in the below code am comparing feature and features1 即在下面的代码中比较功能和功能1

var im = require('imagemagick');

var fs = require("fs");

im.identify('/home/ushu/dev/filesize/colorlaser-246154946-output.pdf', function(err, features) {
  if (err) throw err;
  console.log(features);
  im.identify('/home/ushu/dev/filesize/test.pdf', function(err, features1) {
    if (err) throw err;
    console.log("-------------------------------------------------")
    console.log(features1);
    if (features != features1) {
      console.log("not eqqq")
    }
  });

}); });

console.log(feature) prints the following values console.log(feature)打印以下值

{ '2': '( 3084, 3084, 3084,65535) #0C0C0C0C0C0C rgba(12,12,12,1)',
  '4': '(62451,62451,62451,65535) #F3F3F3F3F3F3 rgba(243,243,243,1)',
    '389490': '(65535,65535,65535,65535) #FFFFFFFFFFFF white'
  format: 'PDF',
  class: 'DirectClass',
  geometry: '612x792+0+0',
  resolution: '72x72',
  'print size': '8.5x11',
  units: 'Undefined',
  type: 'PaletteMatte',
  endianess: 'Undefined',
  colorspace: 'RGB',
  depth: 16,
  'channel depth': { red: '8-bit', green: '8-bit', blue: '8-bit', alpha: '1-bit' },
  'channel statistics': 
   { red: 
      { min: '0 (0)',
        max: '65535 (1)',
        mean: '60171.8 (0.918163)',
        'standard deviation': '15774.2 (0.240699)',
        kurtosis: '7.43399',
        skewness: '-2.96721' },
     green: 
      { min: '0 (0)',
        max: '65535 (1)',
        mean: '60171.8 (0.918163)',
        'standard deviation': '15774.2 (0.240699)',
        kurtosis: '7.43399',
        skewness: '-2.96721' }
     },
  'image statistics': 
   { overall: 
      { min: '0 (0)',
        max: '65535 (1)',
        mean: '44566.9 (0.680048)',
        'standard deviation': '14064.6 (0.214613)',
        kurtosis: '28.682',
        skewness: '-6.99615' } },
  histogram: 
   { '34': '( 2313, 2313, 2313,65535) #090909090909 rgba(9,9,9,1)',
     '289': '( 1799, 1799, 1799,65535) #070707070707 rgba(7,7,7,1)',
     '5755': '(    0,    0,    0,65535) #000000000000 black' },
  'rendering intent': 'Undefined',
  interlace: 'None',
  'background color': 'white',
  'border color': 'rgba(223,223,223,1)',
  'matte color': 'grey74',
  'transparent color': 'none',
  compose: 'Over',
  'page geometry': '612x792+0+0',
  dispose: 'Undefined',
  iterations: '0',
  compression: 'Undefined',
  orientation: 'Undefined',
  properties: 
   { 'date:create': '2014-10-28T12:18:40+06:00',
     'date:modify': '2014-10-28T12:18:40+06:00',
     'pdf:hiresboundingbox': '612x792+0+0',
     'pdf:version': 'PDF-1.3',
     signature: '3a281605e05aa1bd74105887caeaddda4dfac3c78716ac9d1309f3570e4c2368' },
  profiles: 
   { 'profile-icc': '2576 bytes',
     'artifex software srgb icc profile': {} },
  artifacts: { verbose: 'true' },
  tainted: 'False',
  filesize: '59.2KBB',
  'number pixels': '485KB',
  'pixels per second': '48.47MB',
  'user time': '0.020u',
  'elapsed time': '0:01.010',
  version: 'ImageMagick 6.6.9-7 2012-08-17 Q16 http://www.imagemagick.org',
  width: 612,
  height: 792 }

and

console.log(features1) prints console.log(features1)打印

{ '499911': '(65535,65535,65535,    0) #FFFFFFFFFFFF0000 rgba(255,255,255,0)',
  format: 'PDF',
  class: 'DirectClass',
  geometry: '596x842+0+0',
  resolution: '72x72',
  'print size': '8.27778x11.6944',
  units: 'Undefined',
  type: 'Bilevel',
  'base type': 'Bilevel',
  endianess: 'Undefined',
  colorspace: 'RGB',
  depth: 16,
  'channel depth': { gray: '1-bit', alpha: '4-bit' },
  'channel statistics': 
   { gray: 
      { min: '0 (0)',
        max: '65535 (1)',
        mean: '65284.1 (0.996172)',
        'standard deviation': '4046.92 (0.0617521)',
        kurtosis: '256.239',
        skewness: '-16.0698' },
     alpha: 
      { min: '0 (0)',
        max: '65535 (1)',
        mean: '199.892 (0.00305016)',
        'standard deviation': '3454.01 (0.0527048)',
        kurtosis: '329.769',
        skewness: '-18.0427' } },
  alpha: 'rgba(255,255,255,0)   #FFFFFFFFFFFF0000',
  histogram: 
   { '18': '(    0,    0,    0,43690) #000000000000AAAA rgba(0,0,0,0.666667)',
     '24': '(    0,    0,    0,26214) #0000000000006666 rgba(0,0,0,0.4)'
      },
  'rendering intent': 'Undefined',
  interlace: 'None',
  'background color': 'white',
  'border color': 'rgba(223,223,223,1)',
  'matte color': 'grey74',
  'transparent color': 'none',
  compose: 'Over',
  'page geometry': '596x842+0+0',
  dispose: 'Undefined',
  iterations: '0',
  compression: 'Undefined',
  orientation: 'Undefined',
  properties: 
   { 'date:create': '2014-10-28T12:18:41+06:00',
     'date:modify': '2014-10-28T12:18:41+06:00',
     'pdf:hiresboundingbox': '596x842+0+0',
     'pdf:version': 'PDF-1.4',
     signature: '63e1cbdcc1568c376cb5f8612f4034de008add9fe8492e4d378ea176cb8e2eab' },
  profiles: 
   { 'profile-icc': '2576 bytes',
     'artifex software srgb icc profile': {} },
  artifacts: { verbose: 'true' },
  tainted: 'False',
  filesize: '8.21KBB',
  'number pixels': '502KB',
  'pixels per second': '50.18MB',
  'user time': '0.010u',
  'elapsed time': '0:01.009',
  version: 'ImageMagick 6.6.9-7 2012-08-17 Q16 http://www.imagemagick.org',
  width: 596,
  height: 842 }

these printed values are not equal. 这些印刷值不相等。

Using node.js how can i compare these outputted values are equal 使用node.js我如何比较这些输出值是否相等

You can use lodash's isEqual() to compare 2 objects. 您可以使用lodash的isEqual()比较2个对象。 Here is an example from lodash's document: 这是lodash的文档中的示例:

var _ = require('lodash');
var object = { 'name': 'fred' };
var copy = { 'name': 'fred' };

object == copy;
// → false

_.isEqual(object, copy);
// → true

Two objects will return equal in an equality check if they reference the same object in memory, even if the two objects are otherwise identical! 如果两个对象引用内存中的同一对象,则两个对象在相等性检查中将返回相等,即使这两个对象在其他方面相同! So you have to compare the two objects per properties. 因此,您必须比较每个属性的两个对象。

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

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