Đếm có bao nhiêu items giống nhau trong một array

tipjs,object,es6

Sử dụng reduce javascript. Đếm có bao nhiêu items giống nhau trong một array

var cars = ['BMW','Benz', 'Benz', 'Tesla', 'BMW', 'Toyota'];
var carsObj = cars.reduce(function (obj, name) {
  obj[name] = obj[name] ? ++obj[name] : 1;
  return obj;
}, {});
carsObj; // => { BMW: 2, Benz: 2, Tesla: 1, Toyota: 1 }

Có thể bạn đã miss một số snippets code

Using promises to catch errors

Getting the last element of a split string array

Getting the last element of a split string array

Rest Operator for Objects

Now we can use rest on the properties of an Object. It allows us to explicitly extract certain named variables, and assign any uncalled variables into a catchall Object.

encode/decode base64 with JS

Encoding to base64 is done with the btoa command

Convert Array-like to True Array

Convert Array-like to True Array