• About
  • Advertise
  • Privacy & Policy
  • Contact
DevZone
  • Home
  • News
  • Dev
    • All
    • Algorithm
    • Architecture
    • Database
    • Design
    • DevOps
    • IoT
    • Network
    • Test
    • Web
    Phương thức xử lý mảng trong javascript

    Phương thức xử lý mảng trong javascript

    Bảo vệ content, chống copy nội dung web!

    Bảo vệ content, chống copy nội dung web!

    Lưu ý mệnh đề NOT IN trong SQL

    Lưu ý mệnh đề NOT IN trong SQL

    SOLID Principles: Dependency Inversion Principle

    SOLID Principles: Dependency Inversion Principle

    Solid Principles: Interface Segregation Principle

    Solid Principles: Interface Segregation Principle

    Tìm hiểu về composer.json schema (phần 2 – autoload)

    Tìm hiểu về composer.json schema (phần 2 – autoload)

    IPC – Đằng sau sự thành công của Chromium

    IPC – Đằng sau sự thành công của Chromium

    Dựng layout website với CSS Grid Layout

    Dựng layout website với CSS Grid Layout

    Golang cơ bản (p1)

    Golang cơ bản (p1)

    Trending Tags

    • Idea
    • Lifestyle
    No Result
    View All Result
    • Home
    • News
    • Dev
      • All
      • Algorithm
      • Architecture
      • Database
      • Design
      • DevOps
      • IoT
      • Network
      • Test
      • Web
      Phương thức xử lý mảng trong javascript

      Phương thức xử lý mảng trong javascript

      Bảo vệ content, chống copy nội dung web!

      Bảo vệ content, chống copy nội dung web!

      Lưu ý mệnh đề NOT IN trong SQL

      Lưu ý mệnh đề NOT IN trong SQL

      SOLID Principles: Dependency Inversion Principle

      SOLID Principles: Dependency Inversion Principle

      Solid Principles: Interface Segregation Principle

      Solid Principles: Interface Segregation Principle

      Tìm hiểu về composer.json schema (phần 2 – autoload)

      Tìm hiểu về composer.json schema (phần 2 – autoload)

      IPC – Đằng sau sự thành công của Chromium

      IPC – Đằng sau sự thành công của Chromium

      Dựng layout website với CSS Grid Layout

      Dựng layout website với CSS Grid Layout

      Golang cơ bản (p1)

      Golang cơ bản (p1)

      Trending Tags

      • Idea
      • Lifestyle
      No Result
      View All Result
      DEVZONE
      No Result
      View All Result
      Home Dev

      Phương thức xử lý mảng trong javascript

      LapDam by LapDam
      May 31, 2020
      in Dev, Web
      0
      Phương thức xử lý mảng trong javascript
      1. forEach

      Nếu bạn là một lập trình viên thì chắc hẳn bạn không là gì với phương thức này. Nó giúp lắp qua các phần tử trong mảng

       const arr = [1, 2, 3, 4, 5, 6];
      
        arr.forEach(item => {
          console.log(item); // output: 1 2 3 4 5 6
        });

      2. includes

      Phương thức giúp kiểm tra xem phẩn tử có tồn tại trong mảng hay không

        const arr = [1, 2, 3, 4, 5, 6];
      
        arr.includes(2); // output: true
        arr.includes(7); // output: false

      3. filter

      Tạo một mảng mới từ mảng cũ thoả mãn điều kiện

        const arr = [1, 2, 3, 4, 5, 6];
      
        // item(s) greater than 3
        const filtered = arr.filter(num => num > 3);
        console.log(filtered); // output: [4, 5, 6]
      
        console.log(arr); // output: [1, 2, 3, 4, 5, 6]

      4. map

      Tạo một mảng mới khi các phần tử chạy qua 1 function

      const arr = [1, 2, 3, 4, 5, 6];
      
        // add one to every element
        const oneAdded = arr.map(num => num + 1);
        console.log(oneAdded); // output [2, 3, 4, 5, 6, 7]
      
        console.log(arr); // output: [1, 2, 3, 4, 5, 6]

      5. reduce

      Cũng cho các phần tử chạy qua function nhưng trả ra 1 kết quả cuối cùng chứ không phải 1 mảng mới như map

        const arr = [1, 2, 3, 4, 5, 6];
      
        const sum = arr.reduce((total, value) => total + value, 0);
        console.log(sum); // 21
      

      6. some

      Kiểm tra nếu có ít nhất 1 phần tử thoả mãn điều kiện sẽ dừng lại luôn trả về true không thì trả về false

      const arr = [1, 2, 3, 4, 5, 6];
      
        // at least one element is greater than 4?
        const largeNum = arr.some(num => num > 4);
        console.log(largeNum); // output: true
      
        // at least one element is less than or equal to 0?
        const smallNum = arr.some(num => num <= 0);
        console.log(smallNum); // output: false

      7. every

      Hơi khác some, every sẽ duyệt hết cả mảng nếu tất cả thoả mãn điều kiện thì trả vể true, không thì trả về false

       const arr = [1, 2, 3, 4, 5, 6];
      
        // all elements are greater than 4
        const greaterFour = arr.every(num => num > 4);
        console.log(greaterFour); // output: false
      
        // all elements are less than 10
        const lessTen = arr.every(num => num < 10);
        console.log(lessTen); // output: true
      

      8. sort

      Dùng để sắp xếp mảng

        const arr = [1, 2, 3, 4, 5, 6];
        const alpha = ['e', 'a', 'c', 'u', 'y'];
      
        // sort in descending order
        descOrder = arr.sort((a, b) => a > b ? -1 : 1);
        console.log(descOrder); // output: [6, 5, 4, 3, 2, 1]
      
        // sort in ascending order
        ascOrder = alpha.sort((a, b) => a > b ? 1 : -1);
        console.log(ascOrder); // output: ['a', 'c', 'e', 'u', 'y']

      9. Array.from

      Chuyển tham số truyền vào thành mảng

      const name = 'frugence';
        const nameArray = Array.from(name);
      
        console.log(name); // output: frugence
        console.log(nameArray); // output: ['f', 'r', 'u', 'g', 'e', 'n', 'c', 'e']
      // I assume that you have created unorder list of items in our html file.
      
        const lis = document.querySelectorAll('li');
        const lisArray = Array.from(document.querySelectorAll('li'));
      
        // is true array?
        console.log(Array.isArray(lis)); // output: false
        console.log(Array.isArray(lisArray));  // output: true

      10. Array.of

      Biến tất cả các arguments thành mảng

       const nums = Array.of(1, 2, 3, 4, 5, 6);
        console.log(nums); // output: [1, 2, 3, 4, 5, 6]
      

      Hy vọng bài viết này giúp bạn bổ sung thêm kỹ năng khi xử lý mảng trong javascript. Giúp code gọn gàng hơn. Dễ debug hơn.

      Thả tim (5 lượt thả tim)
      Loading...
      Previous Post

      Lợi ích của việc tập thể dục thường xuyên

      Next Post

      Dấu hiệu nhận biết sức khỏe qua liềm móng tay

      LapDam

      LapDam

      Next Post
      Dấu hiệu nhận biết sức khỏe qua liềm móng tay

      Dấu hiệu nhận biết sức khỏe qua liềm móng tay

      Leave a Reply Cancel reply

      Your email address will not be published. Required fields are marked *

      Recent News

      Lập trình viên không dùng máy Mac nhiều như người ta đã nghĩ

      Lập trình viên không dùng máy Mac nhiều như người ta đã nghĩ

      July 25, 2020
      Dấu hiệu nhận biết sức khỏe qua liềm móng tay

      Dấu hiệu nhận biết sức khỏe qua liềm móng tay

      June 26, 2020
      Phương thức xử lý mảng trong javascript

      Phương thức xử lý mảng trong javascript

      May 31, 2020
      Lợi ích của việc tập thể dục thường xuyên

      Lợi ích của việc tập thể dục thường xuyên

      May 25, 2020
      DEVZONE

      Browse by Category

      • Algorithm
      • Architecture
      • Database
      • Design
      • Dev
      • DevOps
      • Idea
      • IoT
      • Lifestyle
      • Network
      • News
      • Test
      • Uncategorized
      • Web
      • About
      • Advertise
      • Privacy & Policy
      • Contact

      © 2019 Devzone

      No Result
      View All Result

      © 2019 Devzone