0

I have a forEach like this

angular.forEach(game.market, function (market) {
  if (availableMarketGroups[market.group_id]) {
      availableMarketGroups[market.group_id].count++;
  }
});

I need to check , if availableMarketGroups[market.group_id] number is duplicate , then ignore it !

For example i have id's : a , b , b , b , c , d --> final count is 6

I need also check , if it's duplicated , then ignore it and get final count 4

  • I believe you just need to remove the duplicates from your array and then perform your loop. See this: http://stackoverflow.com/questions/9229645/remove-duplicates-from-javascript-array – Scott Marcus Apr 16 '17 at 16:12
  • This is a bit confusing. What object is that your screenshot shows? The one with keys `72799064` and `72799067`? – acdcjunior Apr 16 '17 at 16:18
  • .count++; will not work as theres no count var... – Jonas Wilms Apr 16 '17 at 16:26
  • @ScottMarcus data come from swarm , from backend , i can only edit and put it on my html :) –  Apr 16 '17 at 16:49
  • @acdcjunior this id's is my markets id ,the same is availableMarketGroups -> market –  Apr 16 '17 at 16:50
  • @Jonasw the count is work , but its show the wrong number , for example i have x numbers , but its show x+y numbers , becouse some availableMarketGroups[market.group_id] is duplicate –  Apr 16 '17 at 16:51
  • what is game.market and what is availableMarketGroups? looks like they should be the same but ... it's still very unclear what u are trying to achieve. do you want to remove the duplicates or set a mask for further usage within the DOM? – Robin F. Apr 16 '17 at 16:57
  • @RobinF. Hi and thanks for attention . Its really big project and something is unclear in this ticket, but , the main mission is , for example availableMarketGroups[market.group_id] = 1 availableMarketGroups[market.group_id] = 2 availableMarketGroups[market.group_id] = 3 availableMarketGroups[market.group_id] = 3 availableMarketGroups[market.group_id] = 3 availableMarketGroups[market.group_id] = 4 In final count i need number 4 (1,2,3,4) not 6 (1,2,3,3,3,4) –  Apr 16 '17 at 17:02
  • is availableMarketGroups an array containing the amount of groups per group_id? – Robin F. Apr 16 '17 at 17:29
  • I think the overall problem is that it is a sparseArray, making availableMarketGroups.length not work – Jonas Wilms Apr 16 '17 at 17:40
  • From swarm i get random availableMarketGroups[market.group_id] numbers , for example 1 , 2 , 2 , 2 , 3 , 3 --> 6 availableMarketGroups[market.group_id], but i want ignore duplicated id's and get from 1 , 2 , 2 , 2 , 3 , 3 --> 3 (1,2,3) –  Apr 16 '17 at 17:47

3 Answers3

0

You can use a auxiliar structure to store the different group_id.

var summedGroupId = [];
angular.forEach(game.market, function (market) {
    if (availableMarketGroups[market.group_id]) {
        if (summedGroupId.indexOf(market.group_id) < 0) {
            availableMarketGroups[market.group_id].count++;
            summedGroupId.push(market.group_id);
        }
    }
});
María
  • 191
  • 1
  • 5
  • hi , its look like simple and elegant, but count show wrong number again , –  Apr 16 '17 at 16:58
0

So you want

ids=[];
for(key in game.market){
  if(game.market.hasOwnProperty(key)){
       ids.push(game.market[key].group_id);
   }
 }
ids=ids.sort();
uniqueids=ids.filter((el,id)=>(!id||el!=ids[id-1]));
console.log(uniqueids.length);

all the ids in a new array, that is sorted and filtered to be unique, and than you can easily check its length.

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
0

according to your comments i can just guess what you want but availableMarketGroups[market.group_id] means you have the following structure for availableMarketGroups:

{ someGroupId: someValue, someGroupId2: someValue2, ...}

and it looks like you want the values to be unique and filtered for the currents games's markets. (the naming "game.market" does not rly indicate properly that it is actually market"s")


1. approach using reduce

const reducedMarketGroups = availableMarketGroups.reduce((prev, curr) => {
  if (
    game.markets.find(market => market.group_id === curr) && 
    !prev.includes(curr)
  ) prev.push(curr);
  return prev;
}, []);

2. approach using a Set

const reducedMarketGroups = [...new Set(
  availableMarketGroups.filter(marketGroup => 
        game.markets.find(market => market.group_id === marketGroup)
  )
)];
Robin F.
  • 1,137
  • 11
  • 19
  • Thanks for attention :) Yes , but one think , i need only check it, if availableMarketGroups[market.group_id] duplicate or not , not remove it. –  Apr 16 '17 at 18:28