Đế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

Promise + gì

promise + gì

Getting the last element of a split string array

Getting the last element of a split string array

Remove item in array javascript es6

Những bạn nào mà đã sử dụng ES6 một thời gian rồi thì có thể sử dụng method array.filter()

How to reverse an Array?

Reversing an array in JS is really simple with the standard array method. Like below

encode/decode base64 with JS

Encoding to base64 is done with the btoa command