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

Peter The Angular Dude

Related to my other question, where the image is How do I get rid of PROTOTYPE in my new JSON Object from an Array?

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. enter image description here

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 is what's above: IL, WA, CA, and so on are MARKETS like so:

[
    {
      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:

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.

The final JSON should have an additional element:

   displayName: WA,
   name: WA,
   count: 33

And finally, I want to SORT the values of the JSON object which is probably very easy.

UPDATE: A question was raised that the MAP

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

is false. That's not true. It returns the values in the 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:

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

FYI: newMarketArray contains 3300+ objects

Frenchy

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);

let arrResult = Object.keys(objCount).map(k => ({'name': k, 'displayName': k, 'count': objCount[k] }));

console.log(arrResult)

After that, you could sort as you want the dictionary (by the value you want..)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I check if an element exists in array of objects using lodash

How to loop for a specific key for the number of times it occurs in an array of objects

How do I sort this Array of objects using Vanilla or lodash

How do I filter an array of objects by keyword using LoDash?

Can I "count" the number of times a field/name in a json file occurs using python? I do not need the value in the field but the name occurrence

How do I calculate the total number of times a piece of text occurs in a range in Google Sheets using a formula?

How do I count the number of times a sequence occurs in a Java string?

How to find objects inside nested array of objects using lodash?

How to traverse an Array of Objects with multiple elements to make a new array with only the duplicates using Lodash

How do I find number of times a name has been repeated in a list?

Can we find if 0 occurs odd number of times in an array using bit manipulation

How do I find the last element of an array using recursion?

Find the number of times a combination occurs in a numpy 2D array

How do I count the number of times a number appears in an array

find all duplicates on an array of objects using lodash

Find the number of times an element is repeated in List/Array

How do I calculate the number of times a word occurs in a sentence and prints out the indexes? (Python)

How do I count the number of times a value occurs in a csv file with Python?

How do I track the number of times each element is computed in Fibonacci?

How to find the number of times that a substring occurs in a given string (include jointed)?

How to find the number of times a string occurs in a text file?

How do I use grep to find lines, in which any word occurs 3 times?

How to find objects in array where inner array might include other objects by using lodash

How to find objects in array where inner array includes other objects by using lodash

How do i find the peak number of vehicles and peak times

In Javascript, when looping through an array of objects, how do I send each element's name to the console?

How do i find the unique number in an array?

how do I find the longest name in an array of objects, then return the entire object to console in javascript?

Count how many times specific element occurs in array