0

I have two objects below.

As you see, how can I check the objectB belongs to the objectA or not because all the fields of both object are the same, and the casts field of the objectB is a child of the casts field of the objectA.

ObjectA = { 
  title: 'Alita: Battle Angel',
  year: '2019',
  casts:
   [ 'Rosa Salazar',
     'Christoph Waltz',
     'Jennifer Connelly',
     'Mahershala Ali',
     'Michelle Rodriguez',
     'Casper Van Dien' ] }

ObjectB = {
  title: 'Alita: Battle Angel',
  casts: 
     [ 'Rosa Salazar', 
       ' Christoph Waltz' ],
  year: '2019' },
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Nguyen Hoang
  • 540
  • 5
  • 25
  • @mplungjan Thank you for your keyword. I'll do some research. – Nguyen Hoang Mar 12 '19 at 12:40
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every – mplungjan Mar 12 '19 at 12:41
  • 1
    I think a deep compare of objects would help ... but since the data isn't exact you'll have to write special conditions – Doug Mar 12 '19 at 12:41
  • 1
    Hi @NguyenHoang, Kindly check this https://npm.runkit.com/is-subset. Hope it will help you. – Anshul Mar 12 '19 at 12:43
  • 1
    I implemented a test for your objects: https://jsfiddle.net/mplungjan/6aqhcrp8/ - Please note that the leading space in `' Christoph Waltz'` would make the test false – mplungjan Mar 12 '19 at 13:00

3 Answers3

1

The function isObjectBelong receives two arguments:

  • rootObject - object containing cats string array property
  • childObject - object containing cats string array property

Function returns true if childObject.cats contains subset of rootObject.cats, and false otherwise.

const isObjectBelong = (rootObject, childObject) =>
    childObject.casts.every(r => rootObject.casts.includes(r));
Vadim
  • 8,701
  • 4
  • 43
  • 50
punksta
  • 2,738
  • 3
  • 23
  • 41
1

ObjectA = {
  title: 'Alita: Battle Angel',
  year: '2019',
  casts: ['Rosa Salazar',
    'Christoph Waltz',
    'Jennifer Connelly',
    'Mahershala Ali',
    'Michelle Rodriguez',
    'Casper Van Dien'
  ]
}

ObjectB = {
  title: 'Alita: Battle Angel',
  casts: ['Rosa Salazar',
    'Christoph Waltz'
  ],
  year: '2019'
}
ObjectC = {
  title: 'Alita: Battle Angel',
  casts: ['Rosa Salazar',
    'Christoph'
  ],
  year: '2019'
}
ObjectD = {
  title: 'Alita: Battle',
  casts: ['Rosa Salazar',
    'Christoph Waltz'
  ],
  year: '2019'
}
ObjectE = {
  title: 'Alita: Battle Angel',
  casts: ['Rosa Salazar',
    'Christoph Waltz'
  ],
  year: '2018'
}

function compare(obj1, obj2) {

  if (obj1.title == obj2.title && obj1.year == obj2.year) {
    for (i = 0; i < obj2.casts.length; i++) {

      if (obj1.casts.indexOf(obj2.casts[i]) == -1) {
        console.log('obj2 is not a part of obj 1');
        return;
      }
    }
    console.log('obj2 is a part of obj 1');
  } else {
    console.log('obj2 is not a part of obj 1');
  }
}
compare(ObjectA, ObjectB)
compare(ObjectA, ObjectC)
compare(ObjectA, ObjectD)
compare(ObjectA, ObjectE)
Syed Mehtab Hassan
  • 1,297
  • 1
  • 9
  • 23
1

For your specific use case something like the following should work.

ObjectA = {
  title: 'Alita: Battle Angel',
  year: '2019',
  casts: ['Rosa Salazar',
    'Christoph Waltz',
    'Jennifer Connelly',
    'Mahershala Ali',
    'Michelle Rodriguez',
    'Casper Van Dien'
  ]
}

ObjectB = {
  title: 'Alita: Battle Angel',
  casts: ['Rosa Salazar',
    'Christoph Waltz'
  ],
  year: '2019'
}

ObjectC = {
  title: 'Alita: Battle Angel',
  casts: ['Rosa Salazar',
    'Someone else'
  ],
  year: '2019'
}

const isSubset = (parentObject, childObject, isProper) => {
  if ((parentObject.title === childObject.title) && (parentObject.year === childObject.year)) {
    const casts = childObject.casts.filter(cast => parentObject.casts.find(originalCast => originalCast === cast))
    return isProper ? (casts.length === childObject.casts.length) : Boolean(casts.length)
  }
  return false
}

console.log(isSubset(ObjectA, ObjectB))
console.log(isSubset(ObjectA, ObjectC))
console.log(isSubset(ObjectA, ObjectB, true))
console.log(isSubset(ObjectA, ObjectC, true))

I've added a little bit checking and an additional parameter called isProper. isProper will check, whether all the cast in the child exists in the parent. When you add more check your code is less likely to fail.

AshTyson
  • 473
  • 7
  • 23