[Javascript] Làm việc với mảng - Phần 2

//Làm việc với mảng - Phần 2

/**
 * Array methods
 * forEach()
 * every()
 * some()
 * find()
 * filter()
 * map()
 * reduce()
 */
//===============================================================================

var couses = [
   {
      id: 1,
      name: 'Javascript',
      coint: 0
   },
   {
      id: 2,
      name: 'Ruby',
      coint: 500
   },
   {
      id: 3,
      name: 'C#',
      coint: 0
   },
   {
      id: 4,
      name: 'Ruby',
      coint: 400
   },
];

//foreach
   couses.forEach(function (couse, index)//couse: call back
   {

      console.log(index, couse);

   });

//every: true/false
   var isFree = couses.every(function (couse, index)//couse: call back
   {
      return couse.coint == 0;
   });
   console.log(isFree);

//some: true/false <-> every
   var isFree = couses.some(function (couse, index)//couse: call back
   {
      return couse.coint == 0;
   });
   console.log(isFree);

//find: không tìm thấy -> Undefined, trả về 1 phần tử được tìm thấy đầu tiên
   var isRuby = couses.find(function (couse, index)//couse: call back
   {
      return couse.name==="Ruby2";
   });
   console.log(isRuby);

//filter: giống find nhưng trả về tất cả các phần tử được tìm thấy
   var isRuby = couses.filter(function (couse, index)//couse: call back
   {
      return couse.name==="Ruby";
   });
   console.log(isRuby);

   

Nhận xét

Bài đăng phổ biến từ blog này

[SQL Server] Toán tử LIKE và NOT LIKE trong SQL

[Javascript] Object constructor

[SQL Server] Lọc dữ liệu trùng với DISTINCT trong SQL