简体   繁体   English

如何遍历对象数组并使用 Lodash 查找出现次数和元素名称

[英]How do I traverse an array of objects and find the number of times and element name occurs using Lodash

Related to my other question, where the image is How do I get rid of PROTOTYPE in my new JSON Object from an Array?与我的另一个问题相关,图像是如何从数组中删除我的新 JSON Object 中的原型?

I want to be able to do the following:我希望能够做到以下几点:

Taking this one step further...更进一步……

Each of those MARKET (name, displayName) which are both the same for now, have from 1 to "n" number of items within them.每个现在都相同的 MARKET (name, displayName) 中的每一个都有 1 到“n”个项目。 在此处输入图像描述

So, I want to COUNT how many ITEMS are within or repeated.所以,我想计算有多少项目在里面或重复。

For example:例如:

NAME: "IL" may have 300 
NAME: "WA" may have 1000 
NAME: "OR" may have 100 

and so on.

Inside these objects, the word MARKET is there.在这些对象中,有 MARKET 这个词。 Market is what's above: IL, WA, CA, and so on are MARKETS like so:市场就是上面所说的:IL、WA、CA 等等都是这样的市场:

[
    {
      ID: 1,
      NAME: "SomeName1",
      MARKET: "IL",
      LOCATION: "6 Miles east"
    },
    {
      ID: 2,
      NAME: "SomeName2",
      MARKET: "IL",
      LOCATION: "36 Miles east"
    },
    {
      ID: 3,
      NAME: "SomeName3",
      MARKET: "WA",
      LOCATION: "3 Miles west"
    },
    {
      ID: 4,
      NAME: "SomeName4",
      MARKET: "WA",
      LOCATION: "33 Miles west"
    },
    {
      ID: 5,
      NAME: "SomeName5",
      MARKET: "OR",
      LOCATION: "23 Miles north"
    },
    ...
]

I want to add a count to the MAP function I received as a solution:我想为我收到的 MAP function 添加一个计数作为解决方案:

const newObj = newMarketArray.map(e => ({ name: e, displayName: e }));

So that when the values appear as in the image above, I can get the number of occurrences of, WA, IL and OR for example.因此,当值如上图所示时,我可以获得例如 WA、IL 和 OR 的出现次数。

The final JSON should have an additional element:最终的 JSON 应该有一个附加元素:

   displayName: WA,
   name: WA,
   count: 33

And finally, I want to SORT the values of the JSON object which is probably very easy.最后,我想对 JSON object 的值进行排序,这可能很容易。

UPDATE: A question was raised that the MAP更新:提出了一个问题,即 MAP

const newObj = newMarketArray.map(e => ({ name: e, displayName: e })); 

is false.是假的。 That's not true.这不是真的。 It returns the values in the IMAGE.它返回 IMAGE 中的值。 So that is most certainly not false.所以这肯定不是假的。

All I need is to COUNT the number of instances say, CA appears and place it like so:我所需要的只是计算实例的数量,例如 CA 出现并将其放置如下:

const newObj = newMarketArray.map(e => ({ name: e, displayName: e, count: somecount }));

FYI: newMarketArray contains 3300+ objects仅供参考:newMarketArray 包含 3300 多个对象

If i have well understood what you want:如果我很清楚你想要什么:

 newMarketArray = [ { ID: 1, NAME: "SomeName1", MARKET: "IL", LOCATION: "6 Miles east" }, { ID: 2, NAME: "SomeName2", MARKET: "IL", LOCATION: "36 Miles east" }, { ID: 3, NAME: "SomeName3", MARKET: "WA", LOCATION: "3 Miles west" }, { ID: 4, NAME: "SomeName4", MARKET: "WA", LOCATION: "33 Miles west" }, { ID: 5, NAME: "SomeName5", MARKET: "OR", LOCATION: "23 Miles north" } ] let objCount = newMarketArray.reduce((acc, obj) => { acc[obj.MARKET] = (acc[obj.MARKET] || 0) + 1; return acc; }, {}); console.log(objCount); const newObj = newMarketArray.map(e => ({ name: e, displayName: e, COUNT: objCount[e.MARKET]})); console.log(newObj)

After that, you could sort as you want the dictionary (by the value you want..)之后,您可以根据需要对字典进行排序(按您想要的值..)

暂无
暂无

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

相关问题 仅使用 Lodash,我如何隔离数组中出现次数最多的元素? - Using only Lodash, how do i isolate an element that occurs the most in an array? 如何找到每个元素在嵌套数组中出现的最大次数? - How to find the greatest number of times each element occurs in a nested array? 如何使用 Vanilla 或 lodash 对这个对象数组进行排序 - How do I sort this Array of objects using Vanilla or lodash 如何使用LoDash按关键字过滤对象数组? - How do I filter an array of objects by keyword using LoDash? 如何使用 Lodash 遍历具有多个元素的对象数组以创建一个仅包含重复项的新数组 - How to traverse an Array of Objects with multiple elements to make a new array with only the duplicates using Lodash 如何使用 JavaScript 找到出现次数最多的数字? - How can I find the number that occurs most number of times using JavaScript? 如何使用lodash在对象的嵌套数组中查找对象? - How to find objects inside nested array of objects using lodash? 计算值在数组中出现的次数-Javascript-angular-lodash - Count how many times a value occurs in an array - javascript - angular - lodash 使用下划线或lodash或vanilla js,如何确定数组的哪个元素最常出现? - Using underscore or lodash or vanilla js, how to figure out which element of an array occurs most frequently? 使用 lodash 查找对象数组上的所有重复项 - find all duplicates on an array of objects using lodash
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM