简体   繁体   English

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

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

I have the following object...我有以下 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"]
}

I am trying to find a way to get the keys where the values of those keys are the same.我正在尝试找到一种方法来获取这些键的值相同的键。

Desired Output:所需的 Output:

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

If you only need those with legnth > 1如果你只需要那些长度 > 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.

相关问题 比较两个相似的对象(持有相同的键)并返回不同的键值对 - Comparing two similar objects (holding same keys) and returning key value pairs that are different Javascript:合并具有不同键/值对的数组中的对象,并从相同的键中获取总和 - Javascript: Merge Objects in array with different key/value pairs, and getitng the sum from the same keys 将键值对分解为具有相同键的单独对象 - break up key value pairs into separate objects with the same keys 如何在嵌套数组中的键值对中输出键 - How to output keys in key value pairs in nested arrays JavaScript JS-在多个键具有相同值的同时合并其他值时合并对象 - JS - Merge objects when multiple keys have same value while summing other values 当您不将值保存为键:值对而仅作为键时,它会将它们保存为 JS object 中的键,对吗? - When you save values not as key:value pairs but only as key it saves them as keys within a JS object right? 需要合并具有相同键但具有不同值的对象 - Need to merge objects with the same keys but different values 从对象数组中删除键并将值转换为键值对 - Remove keys from object array and turn values into key value pairs 合并两个具有相同键的对象数组,某些对象不会具有相同的值? - Merge two array of objects with same keys, some object won't have the same value? javascript数组何时可以在key:值对中将元素存储为具有“字符串”值的键? - When can a javascript array store elements in key: value pairs with keys having 'string' values?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM