[英]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.