简体   繁体   English

如何在javascript中的对象数组下获取值重复的计数

[英]How to get the count of Value repetition under array of objects in javascript

I have an array which have some repetition count.我有一个数组,它有一些重复计数。 I want to get the count of that repetition.我想获得重复次数。 My array is like this:我的数组是这样的:

var array = [
    { asset: "A", sector: "Hospital" },
    { asset: "B", sector: "Hardware" },
    { asset: "C", sector: "Networking" },
    { asset: "D", sector: "Networking" },
    { asset: "E", sector: "Food" },
    { asset: "F", sector: "Hospital" },
    { asset: "G", sector: "Hardware" },
    { asset: "H", sector: "Industrial" },
    { asset: "I", sector: "Transport" },
    { asset: "J", sector: "Hardware" },
    { asset: "K", sector: "Networking" },
    { asset: "L", sector: "Transport" }
]

Now I want the Repetition count something like this also in sorted order like this:现在我想要重复计数也是这样的排序顺序:

final_array = [
    { sector: 'Hardware', count: 3 },
    { sector: 'Networking', count: 3 },
    { sector: 'Hospital', count: 2 },
    { sector: 'Transport', count: 2 },
    { sector: 'Food', count: 1 },
    { sector: 'Industrial', count: 1 }
]

I don't get the clue from where can do this.我不知道从哪里可以做到这一点。 I got many link but they solve the repetition under array not on Array of objects.我有很多链接,但他们解决了数组下的重复而不是对象数组。

I use the one approach but that not solve my issue我使用一种方法,但不能解决我的问题

var finalD = [];
c.forEach(x => {
    if (isSectorExists(x.sector, finalD) == true) {
        //Here I don't know how I will increase the counter.
    } else {
        finalD.push({ sector: x.sector, count: 1 });
    }
});

var isSectorExists = (sector, arr) => {
    return arr.some(function(el) {
        return el.sector === sector;
    });
};

I know that my approach was very long.我知道我的方法很长。 Is someone have the best and easy approach for doing this kind of task.有人有最好和最简单的方法来完成这种任务吗? Any Help is really Appreciated.任何帮助真的很感激。 Thanks in Advance提前致谢

You can build an object counts with sector name as keys and an object that stores counts as values, iterate through your array and update those counts, and then finally get the values of that counts object.您可以构建一个以扇区名称为键的对象counts和一个将计数存储为值的对象,遍历数组并更新这些计数,然后最终获得该counts对象的值。

 var array = [ { asset: "A", sector: "Hospital" }, { asset: "B", sector: "Hardware" }, { asset: "C", sector: "Networking" }, { asset: "D", sector: "Networking" }, { asset: "E", sector: "Food" }, { asset: "F", sector: "Hospital" }, { asset: "G", sector: "Hardware" }, { asset: "H", sector: "Industrial" }, { asset: "I", sector: "Transport" }, { asset: "J", sector: "Hardware" }, { asset: "K", sector: "Networking" }, { asset: "L", sector: "Transport" } ]; var counts = array.reduce((m, c) => { if (c.sector in m) m[c.sector].count += 1; else m[c.sector] = { sector: c.sector, count: 1}; return m; }, {}); let finalArray = Object.values(counts).sort((a, b) => b.count - a.count); console.log(finalArray);

This will get the counts in linear time (as opposed to calling isSectorExists for every element which would make it O(n^2) .这将在线性时间内获得计数(而不是为每个元素调用isSectorExists使其成为O(n^2)

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

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