2017-03-11 16 views
2

私はオブジェクトを試していますが、object1のキーがある場合は、object2にあるキーを削除しています。ここで2つのオブジェクトを比較し、繰り返すキーを削除する

は一例です。

var original = { 
    a: 1, 
    b: 2, 
    c: 3, 
    e: { 
     tester: 0, 
     combination: { 
      0: 1 
     } 
    }, 
    0: { 
     test: "0", 
     2: "hello" 
    } 
}; 

var badKeys = { 
    a: 1, 
    b: 2, 
    0: { 
     test: "0", 
    } 
} 


var expectedResult = { 
    c: 3, 
    e: { 
     tester: 0, 
     combination: { 
      0: 1 
     } 
    }, 
    0: { 
     2: "hello" 
    } 

} 

私はunderscore差関数を使って試してみたが、それは、これは正しい関数であるかどうかわからないことも、オブジェクトに対して動作しません。

var expectedResultを入手するのに手伝ってもらえますか?

+0

結果は新しいオブジェクトですか? –

+0

はい、元のものを使っても良いでしょう。 –

+0

重複するキーを削除する場合は、「0」を残すのはなぜですか?値が等しい場合はキーのみが削除されますか? – RobG

答えて

1

は、新しいオブジェクトに望んでいたのプロパティをgeetingための反復や再帰的なアプローチを使用することができます。

function deleteKeys(good, bad, result) { 
 
    Object.keys(good).forEach(function (key) { 
 
     if (bad[key] && typeof bad[key] === 'object') { 
 
      result[key] = {}; 
 
      deleteKeys(good[key], bad[key], result[key]); 
 
      return; 
 
     } 
 
     if (!(key in bad) || good[key] !== bad[key]) { 
 
      result[key] = good[key]; 
 
     } 
 
    }); 
 
} 
 

 
var original = { a: 1, b: 2, c: 3, e: { tester: 0, combination: { 0: 1 } }, 0: { test: "0", 2: "hello", another: { a: { B: 2, C: { a: 3 } }, b: 2 } } }, 
 
    badKeys = { a: 1, b: 2, 0: { test: "0", random: 2, another: { a: 1 } } }, 
 
    result = {}; 
 

 
deleteKeys(original, badKeys, result); 
 
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

+0

あなたのコードは深度3でのみ動作し、プロパティは4-5レベル –

+0

同じ問題が発生しました:https://jsfiddle.net/Lg0wyt9u/1677/ –

+0

まだ使用していません... nodejsを使用しているため可能ですか? –

0

for...inループを使用して新しいオブジェクトを返す再帰関数を作成できます。

var original = {"0":{"2":"hello","test":"0"},"a":1,"b":2,"c":3,"e":{"tester":0,"combination":{"0":1}}} 
 
var badKeys = {"0":{"test":"0"},"a":1,"b":2} 
 

 
function remove(o1, o2) { 
 
    var result = {} 
 
    for (var i in o1) { 
 
    if (!o2[i]) result[i] = o1[i] 
 
    else if (o2[i]) { 
 
     if (typeof o1[i] == 'object' && typeof o2[i] == 'object') { 
 
     result[i] = Object.assign(result[i] || {}, remove(o1[i], o2[i])) 
 
     } else if (o1[i] != o2[i]) result[i] = o1[i] 
 
    } 
 
    } 
 
    return result 
 
} 
 

 

 
console.log(remove(original, badKeys))

+0

これは3つ以上のネストされたプロパティを持つ深いオブジェクトでは機能しません –

+0

これはそれだと思うhttps://jsfiddle.net/Lg0wyt9u/1678/ –

+0

そこにあります値が同じではないので、結果に 'another.a'オブジェクトでなければなりませんか? –

0

これは、アルゴリズムのようになります。

あなたの場合に適用
function removeDifferences (original, removeKeys) { 
    // Get keys of to be deleted properties. 
    var keys = Object.keys(removeKeys); 

    // Iterate all properties on removeKeys. 
    for (key of keys) { 
    // Check if property exists on original. 
    if (typeof original[key] !== undefined) { 
     // If the property is an object, call same function to remove properties. 
     if (typeof removeKeys[key] === 'object') { 
     removeDifferences(original[key], removeKeys[key]); 
     } else { 
     delete original[key]; 
     } 
    } 
    } 
    return original; 
} 

/* Your data. */ 
 

 
var original = { 
 
    a: 1, 
 
    b: 2, 
 
    c: 3, 
 
    e: { 
 
    tester: 0, 
 
    combination: { 
 
     0: 1 
 
    } 
 
    }, 
 
    0: { 
 
    test: "0", 
 
    2: "hello" 
 
    } 
 
}; 
 

 
var badKeys = { 
 
    a: 1, 
 
    b: 2, 
 
    0: { 
 
    test: "0", 
 
    } 
 
}; 
 

 
var expectedResult = { 
 
    c: 3, 
 
    e: { 
 
    tester: 0, 
 
    combination: { 
 
     0: 1 
 
    } 
 
    }, 
 
    0: { 
 
    2: "hello" 
 
    } 
 

 
}; 
 

 
/* Function */ 
 

 
function removeDifferences(original, removeKeys) { 
 
    // Get keys of to be deleted properties. 
 
    var keys = Object.keys(removeKeys); 
 

 
    // Iterate all properties on removeKeys. 
 
    for (key of keys) { 
 
    // Check if property exists on original. 
 
    if (typeof original[key] !== undefined) { 
 
     // If the property is an object, call same function to remove properties. 
 
     if (typeof removeKeys[key] === 'object') { 
 
     removeDifferences(original[key], removeKeys[key]); 
 
     } else { 
 
     delete original[key]; 
 
     } 
 
    } 
 
    } 
 
    return original; 
 
} 
 

 
/* Application */ 
 
var output = removeDifferences(original, badKeys); 
 
console.log(output);
.as-console-wrapper { max-height: 100% !important; top: 0; }

0

純粋な関数を使用して、いくつかの再帰と機能プログラミングのための本当の仕事です。 (ノードv7.7.1でテスト済み)

baddictに対応する「リーフ」がある場合、「辞書ツリーのすべてのリーフ」に「whattodo」という関数を適用するための「DoForAllNestedObjects」。

let DoForAllNestedValues = (dict, baddict, whattodo) => { 
    for (let key in dict) { 
     if (typeof (dict[key]) === 'object' && typeof (baddict[key]) === 'object') 
      DoForAllNestedValues(dict[key], baddict[key], whattodo); 
     else 
      if (baddict[key]) 
       whattodo(dict, key); 
    } 
} 

DoForAllNestedValues(original, badKeys, (obj, val) => delete obj[val]); 
console.log(original); 
関連する問題