简体   繁体   English

如何检查某些值是否同时重复?

[英]how to check if certain values are repeated at the same time?

I have an array of values like the one in the example, which I'm going through in a for loop to set certain conditions, but I get to a point where I don't know how to check for repeating values我有一组值,例如示例中的值,我在 for 循环中通过这些值设置某些条件,但我到了不知道如何检查重复值的地步

const objectsData:[
 a:{boxTop = 320,
   boxLeft = 1750,
   boxHeigth = 245,
   boxWidth = 310
 },
 b:{boxTop = 20,
   boxLeft = 580,
   boxHeigth = 245,
   boxWidth = 310
 },
 c:{boxTop = 320,
   boxLeft = 1750,
   boxHeigth = 245,
   boxWidth = 310
 },
 d:{boxTop = 320,
   boxLeft = 1750,
   boxHeigth = 245,
   boxWidth = 310
 },
 e:{boxTop = 136,
   boxLeft = 10,
   boxHeigth = 245,
   boxWidth = 310
 },
]

taking this object array I need to check which of the objects repeat the values "boxLeft" and "boxTop" at the same time (in the example would be objects "a", "d" and "c") to modify them adding a value that would increase in case there are more than two objects that repeat values.使用这个 object 数组我需要检查哪些对象同时重复值“boxLeft”和“boxTop”(在示例中是对象“a”、“d”和“c”)以修改它们添加如果有两个以上的对象重复值,则该值会增加。

any idea is welcome.欢迎任何想法。 Thank you very much in advance非常感谢您提前

1) We need some logic (function) to get the "comparison value" of each object. 1)我们需要一些逻辑(函数)来获得每个object的“比较值”。 For example, given the object a: { boxTop: 320, boxLeft: 1750, boxHeigth: 245, boxWidth: 310 } , as input, we want the value (string) '{"boxTop":320,"boxLeft":1750}' as output, and then we can use that string to compare to other values.例如,给定 object a: { boxTop: 320, boxLeft: 1750, boxHeigth: 245, boxWidth: 310 }作为输入,我们想要值(字符串) '{"boxTop":320,"boxLeft":1750}'作为 output,然后我们可以使用该字符串与其他值进行比较。

2) Given a value, for example the string '{"boxTop":320,"boxLeft":1750}' , we need some logic to count how many times that value appeared in some collection (like a list, dictionary for example). 2) 给定一个值,例如字符串'{"boxTop":320,"boxLeft":1750}' ,我们需要一些逻辑来计算该值在某个集合中出现的次数(例如列表、字典) . There are many ways to approach that problem, I decided to use a dictionary where the key represents some value and the value of the dictionary represents how many times that key appeared in the collection.有很多方法可以解决这个问题,我决定使用一个字典,其中key表示某个值,字典的value表示该键在集合中出现的次数。

Here's a JavaScript (node) implementation of that since you have javascript in your name:这是一个 JavaScript(节点)实现,因为您的名字中有 javascript:

const objectsData = {
    a: { boxTop: 320, boxLeft: 1750, boxHeigth: 245, boxWidth: 310 },
    b: { boxTop: 20, boxLeft: 580, boxHeigth: 245, boxWidth: 310 },
    c: { boxTop: 320, boxLeft: 1750, boxHeigth: 245, boxWidth: 310 },
    d: { boxTop: 320, boxLeft: 1750, boxHeigth: 245, boxWidth: 310 },
    e: { boxTop: 136, boxLeft: 10, boxHeigth: 245, boxWidth: 310 },
};

function getBoxLeftAndBoxTop(obj) {
    const { boxTop, boxLeft } = obj;
    return JSON.stringify({ boxTop, boxLeft });
}

const counterBoxLeftAndBoxTop = {};
for (const key of Object.keys(objectsData)) {
    const box = getBoxLeftAndBoxTop(objectsData[key]);
    const currentCount = counterBoxLeftAndBoxTop[box];
    counterBoxLeftAndBoxTop[box] = currentCount ? currentCount + 1 : 1;
}

console.log(counterBoxLeftAndBoxTop);
/* Outputs:
    {
      '{"boxTop":320,"boxLeft":1750}': 3,
      '{"boxTop":20,"boxLeft":580}': 1,
      '{"boxTop":136,"boxLeft":10}': 1
    }
    */

console.log(`Objects that appears 2 or more times: `);
for (const key of Object.keys(objectsData)) {
    const box = getBoxLeftAndBoxTop(objectsData[key]);
    const currentCount = counterBoxLeftAndBoxTop[box];
    if (currentCount >= 2) {
        console.log(key);
    }
}
/* Outputs:
    Objects that appears 2 or more times:
    a
    c
    d
    */

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

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