繁体   English   中英

当某些键/值对具有不同的键但相同的值时,如何 output 对象键

[英]how to output an objects keys when some key/value pairs have different keys but same values

我有以下 object...

object = {
"A":["1","choiceONE"],
"B":["1","choiceTHREE"],
"C":["1","choiceONE"],
"D":["1","choiceONE"],
"E":["1","choiceTWO"],
"F":["4","choiceONE"],
"G":["4","choiceTHREE"],
"H":["4","choiceTHREE"]
}

我正在尝试找到一种方法来获取这些键的值相同的键。

所需的 Output:

[["A","C","D"], ["G","H"]]

如果你只需要那些长度 > 1

const object = {
    "A":["1","choiceONE"],
    "B":["1","choiceTHREE"],
    "C":["1","choiceONE"],
    "D":["1","choiceONE"],
    "E":["1","choiceTWO"],
    "F":["4","choiceONE"],
    "G":["4","choiceTHREE"],
    "H":["4","choiceTHREE"]
}


function extrangeThing(target){
    let o={};
    for (const property in target) {
        const key=JSON.stringify(target[property]);
        if(o[key]) o[key].push(property);
        else o[key]=[property];
    }
    return Object.values(o).filter(x=>x.length>1);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM