如何检查json中的唯一值

ohtdti5x  于 2021-09-13  发布在  Java
关注(0)|答案(0)|浏览(232)

我正在寻找json对象中部分相同的键/值对。如果它们不相同,则添加到下一个索引。在这种情况下,类似的值将是“a”:“a”和“b”:“b”。我在es5工作
我得到一个值来比较(a,b,c)。我将查看以下值:

// test #
{ "a": "a", "b": "b", "c":"1"}  // 0
{ "a": "a", "b": "b", "c":"2"}  // 1
{ "a": "aa", "b": "b", "c":"3"} // 2
{ "a": "32", "b": "b", "c":"4"} // 3
{ "a": "aa", "b": "b", "c":"5"} // 4

我从一个空数组[]开始,并希望最终结果为

// groups with similar values
// groups = [[{"a": "a", "b": "b", "c":"1"}, {"a": "a", "b": "b", "c":"3"}], [{"a": "aa", "b": "b", "c":"3"}, { "a": "aa", "b": "b", "c":"5"}], [{ "a": "32", "b": "b", "c":"4"}]]

第一个数组被分组,因为“a”和“b”的键/值对相同。然后制作另一个数组以包含唯一的“a”和“b”。
处理这个问题的最佳方法是什么?我有一个混乱的循环,但不知道它是否可以改进?

function foo(test)
    loop through groups, if empty then add test 0 json
    if not empty, then look through array of json elements in each index of the outer array
        then see if there's a matching "a" and "b"
        if matching, then add test # to that inner array
        else make a new inner array with the test #
}

我真的不确定上面的说法是否正确(或者如何写)。我能得到一些帮助吗?谢谢

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题