Wondering what’s next for npm?Check out our public roadmap! »

    TypeScript icon, indicating that this package has built-in type declarations

    0.3.6 • Public • Published

    Compare anything 🛰

    npm i compare-anything

    Compares objects and arrays and tells you which props or values are duplicates, and which are only present once.

    It works just like you would compare two columns in excel! But who needs excel when you've got JavaScript, am I right? 😃

    Meet the family


    It works just like you would expect. You compare(objectA, objectB) and it gives you all kind of info.

    You can do all kind of things with compare-anything!

    • Compare object props, to see which props are present in which objects

    Compare object props

    Which props are present in which objects. Remember, this function only looks at the prop-names! Not the values.

    Will return an info object with:

    • props - an array with all props of all objects
    • presentInAll - is the prop present in all passed objects? true/false per prop
    • perProp - an array of objects per prop that had that specific prop
    • presentIn - an array of indexes per prop that had that specific prop (indexes of the params you passed to the function)
    import { compareObjectProps } from 'compare-anything'
    // only props 'b' and 'c' are present in both ↓
    const objectA = {a: '🎴', b: '🎴', c: '🎴'}
    const objectB = {b: '🀄️', c: '🀄️', d: '🀄️'}
    compareObjectProps(objectA, objectB)
    // returns ↓
      props: ['a', 'b', 'c', 'd'],
      presentInAll: { a: false, b: true, c: true, d: false },
      perProp: { a: [objectA], b: [objectA, objectB], c: [objectA, objectB], d: [objectB] },
      presentIn: { a: [0], b: [0, 1], c: [0, 1], d: [1] }

    Compare more than two

    You can pass as many arguments as you want!

    // keep on adding objects to compare!
    compareObjectProps(objectA, objectB, objectC, objectD, objectE)

    Compare objects in an array

    When you need to compare objects in an array you can use destructuring:

    // you can compare an array of objects like so:

    Find duplicates based on one prop value

    When you need to find duplicate objects based on one single prop value of that object, you can easily do so as follows:

      ...arrayOfObjects.map(obj => {
        return { [obj.idField]: obj }

    In the example above you can change idField by the actual prop name you need. By making a key out of the value you can easily find duplicates based on just this field.

    Nested props

    If we require to check even nested props we can use the flatten-anything function like shown below:

    import flatten from 'flatten-anything'
    import { compareObjectProps } from 'compare-anything'
    const objectA = {nested: {a: '🎴', b: '🎴'}}
    const objectB = {nested: {a: '🀄️', c: '🀄️'}}
    const flatA = flatten(objectA)
    // → {'nested.a': '🎴', 'nested.b': '🎴'}
    const flatB = flatten(objectB)
    // → {'nested.a': '🀄️', 'nested.c': '🀄️'}
    compareObjectProps(flatA, flatB)
    // returns ↓
      props: ['nested.a', 'nested.b', 'nested.c'],
      presentInAll: { 'nested.a': true, 'nested.b': false, 'nested.c': false },
      perProp: { 'nested.a': [objectA, objectB], 'nested.b': [objectA], 'nested.c': [objectB] },
      presentIn: { 'nested.a': [0, 1], 'nested.b': [0], 'nested.c': [1] }


    npm i compare-anything

    DownloadsWeekly Downloads






    Unpacked Size

    38 kB

    Total Files


    Last publish


    • avatar