Home > Software design >  How do I traverse an array of objects and find the number of times and element name occurs using Lod
How do I traverse an array of objects and find the number of times and element name occurs using Lod

Time:11-25

Related to my other question, where the image is 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

CodePudding user response:

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)
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

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

  • Related