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

前端 未结 5 671
日久生厌
日久生厌 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:15

    // When order of objects is not same

    function isContainedIn(a, b) {
        if (typeof a != typeof b)
            return false;
        if (Array.isArray(a) && Array.isArray(b)) {
            if(a.length == 1) {
                var j=0;
                while (j < b.length) {
                    if ((isContainedIn( a[0], b[j]))) {
                        return true;
                    }
                    j++;
                }
                return false;
            } else {
                var k=0;
                while (k < a.length) {
                    if (!(isContainedIn([a[k]], b))) {
                        return false;
                    }
                    k++;
                }
                return true;
            }
        } else if (Object(a) === a) {
            for (var p in a)
                if (!(p in b && isContainedIn(a[p], b[p])))
                    return false;
            return true;
        } else
            return a === b;
    };
    
    
    isContainedIn(requirements, person)
    true
    

提交回复
热议问题