简体   繁体   English

如何计算数组和 console.log 中每个重复元素的数量?

[英]How can I count the amount of each repeating element in an array and console.log it?

I have an array of Objects where each object has an ID property.我有一个对象数组,其中每个对象都有一个 ID 属性。 How can I count the number of elements with the same ID property in my array?如何计算数组中具有相同 ID 属性的元素数量?

My current logic that I've tried is to compare the ID of the current element in the loop with all the other ones and if the IDs match, I replace that element in the array with null and increment the counter by one.我目前尝试的逻辑是将循环中当前元素的 ID 与所有其他元素的 ID 进行比较,如果 ID 匹配,则将数组中的该元素替换为null并将计数器加一。

The unique ID properties of each object in myArray are item_id while in the itemDB object they are id . myArray中每个对象的唯一 ID 属性是item_id而在itemDB对象中它们是id

let field = '';
for (let i = 0; i < myArray.length; i++) {
                if (myArray[i] == null) continue;
                
                // db.items.findByPk() gets an object from the database that has the property `id`
                let itemDB = await db.items.findByPk(myArray[i].get('item_id'));
                let count = 1

    
                for (let j = i + 1; j < myArray.length; j++) {
                    if (myArray[j].get('item_id') == itemDB.get('id')) {
                        count += 1;
                        myArray[j] = null;
                    }
                }
    
                if (myArray[i].get('location') !== 'storage') weight += count * itemDB.get('weight')
                field += `${count}x - ${itemDB.get('name')}\n`
                    
            }

EDIT: I'd prefer if someone could offer me insight on a completely different approach.编辑:如果有人可以为我提供完全不同的方法的见解,我更愿意。 What I basically need is to be able to count an occurrence of each element in the array (but instead of comparing the elements directly, I'm comparing their unique ID properties since the elements are objects)我基本上需要的是能够计算数组中每个元素的出现次数(但我不是直接比较元素,而是比较它们的唯一 ID 属性,因为元素是对象)

Well without knowing much about your code, just counting duplicates would be as easy as:好吧,在对您的代码了解不多的情况下,只需计算重复项就很简单:

String[] strArray = ...;
int duplicateAmount = 0;
for(int i = 0; i < strArray.length(); i++) {
    for(int j = 0; j < strArray.length(); j++) {
        if(strArray[i] == strArray[j]) {
            duplicateAmount++;
        }
     }
}

If you wanted to know many different duplicates you can of course just make the duplicateAmounts an array instead where the index is equal to the index of whichever object there is a duplicate of.如果您想知道许多不同的重复项,您当然可以将 duplicateAmounts 设为一个数组,而不是其中索引等于存在重复项的任何对象的索引。

Hope this helped!希望这有帮助!

(and to just be checking the ID's of something you'd of course do if(objArray[i].getId() == objArray[j].getId()) ) (并且只是检查你当然会做的东西的ID if(objArray[i].getId() == objArray[j].getId())

ALSO: I'm an idiot for writing the response in Java lol (force of habit) but hopefully it's understandable.另外:我是个用 Java 写回复的白痴(习惯的力量),但希望这是可以理解的。 :) :)

Lets assume you have an array of IDs假设您有一组 ID

array = [{id: 'a'}, {id: 'b'}, ...]

function getRepeatedIds(array){
    return array.map((element, index) => {
        let newArray = array;
        return newArray.filter((repeatedElements, otherIndex) => element[index].id === repeatedElements[otherIndex].id ));
}) 
}

The result should be an array where in each position is an array of elements with that ID结果应该是一个数组,其中每个位置都是具有该 ID 的元素数组

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

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