javascript - Store the occurrence in an array -


I'm looking for an effective way to count the incidence of elements I read data in a loop, and each In the phase I want to increase the right object element in the result array, or create a new one, if it is not yet available.

I have to work with so many data, so I need a quick solution here is a working version:

  var hugeDataObject = [{ID: '1234 ', Dark: true}, {id:' 5678 ', dark: true}, {id:' 91011 ', dark: true}, {id:' 91011 ', dark: false}]; Var ID = []; Var dark = []; Var allIds = []; Var allDarks = []; HugeDataObject.forEach (works) (var index = allIds.indexOf (attrs.id); if (index> = 0) id [index]. Sum + = 1; other {ids.push ({type: attrs ID, zodiac: 1}); allIds.push (attrs.id);} var index = allDarks.indexOf (attrs.dark); if (index> = 0) dark [index] amount + = 1; other {Darks.push ({type: attrs.dark, zodiac: 1}); allDarks.push (attrs.dark);}});  

But I have more types, which I need to count, so there is a lot of variables.

Result:

  id = [{type: '1234', amount: 1}, {type: '5678', amount: 1}, {type: 91011 ', sum: 2}] dark = [{type: true, amount: 3}, {type: false, amount: 1}]  

(if you use loadish If so, this is okay)

Thanks Advance!

How to store a simple structure:

  Var objects = {}; Objects ['id 1234'] = 384; Objects ['id5678'] = 955; Objects ['id91011'] = 1510; / * Increment * / objects ['id1234'] ++;  

Comments

Popular posts from this blog

sqlite3 - UPDATE a table from the SELECT of another one -

c# - Showing a SelectedItem's Property -

javascript - Render HTML after each iteration in loop -