Đế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 javascript callback style error handling

How to use the spread operator to flatten array of objects in javascript

How to use the spread operator to flatten array of objects in javascript

try/catch es10

An update for developer convience allows the use of try/catch without an explicit e Error reference in the catch call.

Promise + gì

promise + gì

serialize Form

Sử dụng formdata constructor để convert form, FormData, array.from(). Kết hợp window.encodeURIComponent() để encode từng value trong form.