JavaScript How can I count the occurrence of a value of an attribute in an array of anonymous objects count value occurrence sql,count occurrence of value in excel,

You can use reduce() and return object as result.

var items = new Array();
items.push({x: 1, y: "what"});
items.push({x: 3, y: "ever"});
items.push({x: 4, y: "can"});
items.push({x: 4, y: "happen"});
items.push({x: 1, y: "will"});
items.push({x: 4, y: "happen"});

var result = items.reduce(function(r, o) {
  r[o.x] = (r[o.x] || 0) + 1;
  return r;
}, {})

console.log(result)
Answer:1

No need for Array#reduce, an Array#forEach works well.

The result is an object. the reference does not change, so it is not necessary to use something which change the reference to a result.

If, for examle, a result is made of a countinuous addition, then the result would change, but here, with an object, it does not change. It is more some kind of laziness, to move an object through the reduce.

var items = [{ x: 1, y: "what" }, { x: 3, y: "ever" }, { x: 4, y: "can" }, { x: 4, y: "happen" }, { x: 1, y: "will" }, { x: 4, y: "happen" }],
    count = Object.create(null);

items.forEach(function(a) {
    count[a.x] = (count[a.x] || 0) + 1;
});

console.log(count);
Answer:2

A quick and sorted solution :

var items = new Array();
items.push({x: 1, y: "what"});
items.push({x: 3, y: "ever"});
items.push({x: 4, y: "can"});
items.push({x: 4, y: "happen"});
items.push({x: 1, y: "will"});
items.push({x: 4, y: "happen"});
var res = items.map(x => ({x: x.x})).sort((a,b) => a.x - b.x).reduce((r,o) => {
    r[o.x] = (r[o.x] || 0) + 1;
    return r;
}, {});
console.log(res); //{ '1': 2, '3': 1, '4': 3 }
Answer:3

I am using aldeed:autoform I have these Schemas Schema.UserProfile = new SimpleSchema({ fullName: { type: String, optional: true }, bio: { type: String, optional: true } Schema.User =...

I am using aldeed:autoform I have these Schemas Schema.UserProfile = new SimpleSchema({ fullName: { type: String, optional: true }, bio: { type: String, optional: true } Schema.User =...

I read in a book that we can extend functionality of existing objects using the following code: var Point = function(x, y) { this.x = x; this.y = y; } Point.prototype.moveBy = function(...

I read in a book that we can extend functionality of existing objects using the following code: var Point = function(x, y) { this.x = x; this.y = y; } Point.prototype.moveBy = function(...

  1. what difference between bourbon and whiskey
  2. what difference between vegan and vegetarian
  3. what difference between psoriasis and eczema
  4. what difference between flu a and b
  5. what difference between ipad and ipad air
  6. what difference between republic and democracy
  7. what difference between inc and llc
  8. what difference between chromebook and laptop
  9. what difference between md and do
  10. what difference between virus and bacteria
  11. what difference between w2 and w4
  12. what difference between perfume and eau de toilette
  13. what difference between cold and flu
  14. what difference between catholic and christian
  15. what difference between diabetes 1 and 2
  16. what difference between california king and king
  17. what difference between macbook air and pro
  18. what difference between airpod 1 and 2
  19. what difference between medicare and medicaid
  20. what difference between bipolar 1 and 2

I have a Grid and on grid context menu I am calling one function as follows- On grids context menu I have added the following item { text: 'Preview', handler: 'PreviewGrid', scope: cnt, }; In ...

I have a Grid and on grid context menu I am calling one function as follows- On grids context menu I have added the following item { text: 'Preview', handler: 'PreviewGrid', scope: cnt, }; In ...

For example: var array1 = ["one", "two", "three"]; var array2 = ["1", "2", "3"]; How to turn these arrays to var array = [["one", "1"], ["two", "2"], ["three", "3"]]?

For example: var array1 = ["one", "two", "three"]; var array2 = ["1", "2", "3"]; How to turn these arrays to var array = [["one", "1"], ["two", "2"], ["three", "3"]]?