2014-10-28 258 views
0

我試圖用node.js比較兩個對象。 即在下面的代碼中比較功能和特點1使用node.js比較兩個對象

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我怎麼能比較這些輸出值相等

+0

您可以使用「Underscore」軟件包來比較兩個對象 – Subburaj 2014-10-28 07:00:05

回答

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