concat

  • Document: Array.prototype.concat() - JavaScript | MDN (mozilla.org)

  • Use case: khi cần nối hai mảng lại với nhau.

  • Example:

    const oneToFive = [1, 2, 3, 4, 5]
    const sixToTen = [6, 7, 8, 9, 10]
     
    console.log(oneToFive.concat(sixToTen)) // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    console.log(sixToTen.concat(oneToFive)) // [6, 7, 8, 9, 10, 1, 2, 3, 4, 5]

Lưu ý: concat không làm thay đổi mảng gốc ban đầu.

indexOf

  • Document: Array.prototype.indexOf() - JavaScript | MDN (mozilla.org)

  • Use case:

    • Kiểm tra mảng có tồn tại phần tử (nếu hàm trả về là -1 thì tức là không tồn tại).
    • Tìm vị trí index của một phần tử bất kỳ.
  • Example:

    const ASIA = ["China", "Viet Nam", "Japan", "Thailand", "Korean"]
     
    console.log(ASIA.indexOf("USA")) // -1
    console.log(ASIA.indexOf("Viet Nam")) // 1
    console.log(ASIA.indexOf("Viet nam")) // - 1
    console.log(ASIA.indexOf("Korean")) // 4

includes

  • Document: Array.prototype.includes() - JavaScript | MDN (mozilla.org)

  • Use case: khi cần kiểm tra xem một phần tử có trong mảng hay không.

  • Example:

    const ASIA = ["China", "Viet Nam", "Japan", "Thailand", "Korean"]
     
    console.log(ASIA.includes("china")) // false
    console.log(ASIA.includes("Viet Nam")) // true

join

  • Document: Array.prototype.join() - JavaScript | MDN (mozilla.org)

  • Use case: khi cần chuyển mảng thành một chuỗi, cách nhau bởi delimeter tùy ý. Giá trị mặc định của delimeter là dấu ,.

  • Example:

    const greeting = ["H", "e", "l", "l", "o"]
    const delimeter = ""
     
    console.log(greeting.join()) // "H,e,l,l,o"
    console.log(greeting.join(delimeter)) // "Hello"
    console.log(greeting.join(", ")) // "H, e, l, l, o"

slice

  • Document: Array.prototype.slice() - JavaScript | MDN (mozilla.org)

  • Use case: khi cần cắt lấy mảng con từ mảng chính.

  • Example:

    const countries = ["USA", "France", "England", "Viet Nam", "China", "Thailand"]
    const westCountries = countries.slice(0, 3)
    const asiaCountries = countries.slice(3, 7)
     
    console.log(westCountries) // ["USA", "France", "England"]
    console.log(asiaCountries) // ["Viet Nam", "China", "Thailand"]

Lưu ý: slice không làm thay đổi mảng gốc ban đầu.

splice

  • Document: Array.prototype.splice() - JavaScript | MDN (mozilla.org)

  • Use case: khi cần thêm/xóa/sửa một hoặc nhiều phần tử ở các vị trí bất kỳ.

  • Example:

    const asiaCountries = ["USA", "France", "England", "viet Nam", "China", "Thailand"]
     
    // Delete first three countries from the beginning
    console.log(asiaCountries.splice(0, 3)) // ["USA", "France", "England"]
    console.log(asiaCountries) // ["viet Nam", "China", "Thailand"]
     
    // Add new countries to the ending
    asiaCountries.splice(asiaCountries.length, 0, "Japan", "Korean")
    console.log(asiaCountries) // ["viet Nam", "China", "Thailand", "Japan", "Korean"]
     
    // Modify "viet Nam" to "Viet Nam"
    asiaCountries.splice(0, 1, "Viet Nam")
    console.log(asiaCountries) // ["Viet Nam", "China", "Thailand", "Japan", "Korean"]

Lưu ý: splice làm thay đổi mảng gốc ban đầu. Giá trị trả về của splice là các phần tử bị xóa.

Others

Các phương thức khác:

const array = []
 
// Add new elements to the ending of an array.
// Return: new length of the array.
console.log(array.push(1, 2, 3)) // 3, current array: [1, 2, 3]
 
// Remove last element from an array.
// Return: removed element.
console.log(array.pop()) // 3, current array: [1, 2]
 
// Add new elements to the beginning of an array.
// Return: new length of the array.
console.log(array.unshift(3, 4, 5)) // 5, current array: [3, 4, 5, 1, 2]
 
// Remove first element from an array
// Return: removed element.
console.log(array.shift()) // 3, current array: [4, 5, 1, 2]
 
// Reverse an array
// Return: reversed array
console.log(array.reverse()) // [2, 1, 5, 4]