使用node.js比较两个对象

时间:2014-10-28 06:56:57

标签: javascript node.js imagemagick

尝试使用node.js比较两个对象。 即在下面的代码中比较特征和特征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)打印以下值

{ '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 }

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 }

这些印刷值不相等。

使用node.js如何比较这些输出值是否相等

2 个答案:

答案 0 :(得分:1)

您可以使用lodash's isEqual()来比较2个对象。以下是lodash的文档中的一个示例:

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

object == copy;
// → false

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

答案 1 :(得分:0)

如果两个对象在内存中引用相同的对象,则它们将在相等性检查中返回相等,即使这两个对象在其他方面是相同的!所以你必须比较每个属性的两个对象。