Object Comparing: check if an object contains the whole other object

前端 未结 5 705
日久生厌
日久生厌 2021-01-18 19:51

I have two objects. Their structure looks a bit like this:

{
 education: [\"school\", \"institute\"],
 courses: [\"HTML\", \"JS\", \"CSS\"],
 Computer: {
            


        
5条回答
  •  不要未来只要你来
    2021-01-18 20:03

    Just recursively check it:

    function isContainedIn(a, b) {
        if (typeof a != typeof b)
            return false;
        if (Array.isArray(a) && Array.isArray(b)) {
            // assuming same order at least
            for (var i=0, j=0, la=a.length, lb=b.length; i

    > isContainedIn(requirements, person)
    true
    

    For a more set-logic-like approach to arrays, where order does not matter, add something like

            a.sort();
            b = b.slice().sort()
    

    (assuming orderable contents) before the array comparison loop or replace that by the quite inefficient

            return a.every(function(ael) {
                return b.some(function(bel) {
                    return isContainedIn(ael, bel);
                });
            });
    

提交回复
热议问题