Hướng dẫn how do i slice a number in javascript? - làm cách nào để cắt một số trong javascript?

Ví dụ

Cắt 5 vị trí đầu tiên:

hãy để văn bản = "Hello World!"; Đặt kết quả = text.slice (0, 5);
let result = text.slice(0, 5);

Hãy tự mình thử »

Từ vị trí 3 đến cuối:

Đặt kết quả = text.slice (3);

Hãy tự mình thử »

Từ vị trí 3 đến cuối:


Đặt kết quả = text.slice (3);

Thêm ví dụ dưới đây.

Định nghĩa và cách sử dụng

Phương pháp slice() trích xuất một phần của chuỗi.

Phương thức slice() trả về phần được trích xuất trong một chuỗi mới.

Phương thức slice() không thay đổi chuỗi gốc.

Các tham số bắt đầu và kết thúc chỉ định một phần của chuỗi để trích xuất.


Vị trí đầu tiên là 0, vị trí thứ hai là 1, ...

Một số âm chọn từ cuối chuỗi.

Cú phápThông số
Tham sốSự mô tả
The start position.
(First character is 0).
bắt đầuYêu cầu. Vị trí bắt đầu. (Ký tự đầu tiên là 0).
The end position (up to, but not including).
Default is string length.

chấm dứt

Tùy chọn. Vị trí cuối (lên đến, nhưng không bao gồm). Mặc định là độ dài chuỗi.Thông số
Tham sốSự mô tả


bắt đầu


Yêu cầu. Vị trí bắt đầu. (Ký tự đầu tiên là 0).

chấm dứt

Tùy chọn. Vị trí cuối (lên đến, nhưng không bao gồm). Mặc định là độ dài chuỗi.

Giá trị trả vềLoại hìnhMột chuỗiPhần trích xuất của chuỗi.Nhiều ví dụ hơnHỗ trợ trình duyệt
slice() là tính năng ECMAScript1 (ES1).slice() là tính năng ECMAScript1 (ES1).slice() là tính năng ECMAScript1 (ES1).slice() là tính năng ECMAScript1 (ES1).slice() là tính năng ECMAScript1 (ES1).slice() là tính năng ECMAScript1 (ES1).


Ví dụ

Chọn các yếu tố:

const fruits = ["chuối", "cam", "chanh", "táo", "xoài"]; const citrus = trái cây.slice (1, 3);
const citrus = fruits.slice(1, 3);

Hãy tự mình thử »

Chọn các phần tử bằng cách sử dụng các giá trị âm:

const fruits = ["chuối", "cam", "chanh", "táo", "xoài"]; const mybest = fruits.slice (-3, -1);
const myBest = fruits.slice(-3, -1);

Hãy tự mình thử »


Chọn các phần tử bằng cách sử dụng các giá trị âm:

const fruits = ["chuối", "cam", "chanh", "táo", "xoài"]; const mybest = fruits.slice (-3, -1);

Định nghĩa và cách sử dụng

Phương thức slice() trả về các phần tử được chọn trong một mảng, dưới dạng mảng mới.


Phương pháp slice() chọn từ một khởi đầu nhất định, cho đến một (không bao gồm) kết thúc được đưa ra.

Phương pháp slice() không thay đổi mảng gốc.

Cú phápThông số
Tham sốSự mô tả
Start position. Default is 0.
Negative numbers select from the end of the array.
bắt đầuTùy chọn. Vị trí khởi động. Mặc định là 0. Số âm Chọn từ cuối mảng.
End position. Default is last element.
Negative numbers select from the end of the array.

chấm dứt

Tùy chọn. Vị trí. Mặc định là yếu tố cuối cùng. Số âm Chọn từ cuối mảng.



Giá trị trả về

Một mảng mới chứa các yếu tố đã chọn.

Hỗ trợ trình duyệt

slice() là tính năng ECMAScript1 (ES1).ES1 (JavaScript 1997) được hỗ trợ đầy đủ trong tất cả các trình duyệt:Trình duyệt ChromeI EBờ rìaFirefox
Cuộc đi sănCuộc đi sănCuộc đi sănCuộc đi sănCuộc đi sănCuộc đi săn

Phương thức slice() trả về một bản sao nông của một phần của một mảng vào một đối tượng mảng mới được chọn từ

const fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'];
const citrus = fruits.slice(1, 3);

// fruits contains ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
// citrus contains ['Orange','Lemon']
6 đến
const fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'];
const citrus = fruits.slice(1, 3);

// fruits contains ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
// citrus contains ['Orange','Lemon']
7 (không bao gồm
const fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'];
const citrus = fruits.slice(1, 3);

// fruits contains ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
// citrus contains ['Orange','Lemon']
7) trong đó
const fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'];
const citrus = fruits.slice(1, 3);

// fruits contains ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
// citrus contains ['Orange','Lemon']
6 và
const fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'];
const citrus = fruits.slice(1, 3);

// fruits contains ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
// citrus contains ['Orange','Lemon']
7 đại diện cho chỉ số của các mục trong mảng đó. Mảng ban đầu sẽ không được sửa đổi.slice() method returns a shallow copy of a portion of an array into a new array object selected from
const fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'];
const citrus = fruits.slice(1, 3);

// fruits contains ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
// citrus contains ['Orange','Lemon']
6 to
const fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'];
const citrus = fruits.slice(1, 3);

// fruits contains ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
// citrus contains ['Orange','Lemon']
7 (
const fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'];
const citrus = fruits.slice(1, 3);

// fruits contains ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
// citrus contains ['Orange','Lemon']
7 not included) where
const fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'];
const citrus = fruits.slice(1, 3);

// fruits contains ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
// citrus contains ['Orange','Lemon']
6 and
const fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'];
const citrus = fruits.slice(1, 3);

// fruits contains ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
// citrus contains ['Orange','Lemon']
7 represent the index of items in that array. The original array will not be modified.

Thử nó

Cú pháp

slice()
slice(start)
slice(start, end)

Thông số

const fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'];
const citrus = fruits.slice(1, 3);

// fruits contains ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
// citrus contains ['Orange','Lemon']
6 Tùy chọnOptional

Chỉ số dựa trên không tại đó để bắt đầu trích xuất, được chuyển đổi thành một số nguyên.

  • Chỉ số âm tính trở lại từ cuối mảng - nếu
    // Using slice, create newCar from myCar.
    const myHonda = { color: 'red', wheels: 4, engine: { cylinders: 4, size: 2.2 } };
    const myCar = [myHonda, 2, 'cherry condition', 'purchased 1997'];
    const newCar = myCar.slice(0, 2);
    
    console.log('myCar =', myCar);
    console.log('newCar =', newCar);
    console.log('myCar[0].color =', myCar[0].color);
    console.log('newCar[0].color =', newCar[0].color);
    
    // Change the color of myHonda.
    myHonda.color = 'purple';
    console.log('The new color of my Honda is', myHonda.color);
    
    console.log('myCar[0].color =', myCar[0].color);
    console.log('newCar[0].color =', newCar[0].color);
    
    2,
    // Using slice, create newCar from myCar.
    const myHonda = { color: 'red', wheels: 4, engine: { cylinders: 4, size: 2.2 } };
    const myCar = [myHonda, 2, 'cherry condition', 'purchased 1997'];
    const newCar = myCar.slice(0, 2);
    
    console.log('myCar =', myCar);
    console.log('newCar =', newCar);
    console.log('myCar[0].color =', myCar[0].color);
    console.log('newCar[0].color =', newCar[0].color);
    
    // Change the color of myHonda.
    myHonda.color = 'purple';
    console.log('The new color of my Honda is', myHonda.color);
    
    console.log('myCar[0].color =', myCar[0].color);
    console.log('newCar[0].color =', newCar[0].color);
    
    3 được sử dụng.
  • Nếu
    // Using slice, create newCar from myCar.
    const myHonda = { color: 'red', wheels: 4, engine: { cylinders: 4, size: 2.2 } };
    const myCar = [myHonda, 2, 'cherry condition', 'purchased 1997'];
    const newCar = myCar.slice(0, 2);
    
    console.log('myCar =', myCar);
    console.log('newCar =', newCar);
    console.log('myCar[0].color =', myCar[0].color);
    console.log('newCar[0].color =', newCar[0].color);
    
    // Change the color of myHonda.
    myHonda.color = 'purple';
    console.log('The new color of my Honda is', myHonda.color);
    
    console.log('myCar[0].color =', myCar[0].color);
    console.log('newCar[0].color =', newCar[0].color);
    
    4 hoặc
    const fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'];
    const citrus = fruits.slice(1, 3);
    
    // fruits contains ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
    // citrus contains ['Orange','Lemon']
    
    6 bị bỏ qua,
    // Using slice, create newCar from myCar.
    const myHonda = { color: 'red', wheels: 4, engine: { cylinders: 4, size: 2.2 } };
    const myCar = [myHonda, 2, 'cherry condition', 'purchased 1997'];
    const newCar = myCar.slice(0, 2);
    
    console.log('myCar =', myCar);
    console.log('newCar =', newCar);
    console.log('myCar[0].color =', myCar[0].color);
    console.log('newCar[0].color =', newCar[0].color);
    
    // Change the color of myHonda.
    myHonda.color = 'purple';
    console.log('The new color of my Honda is', myHonda.color);
    
    console.log('myCar[0].color =', myCar[0].color);
    console.log('newCar[0].color =', newCar[0].color);
    
    6 được sử dụng.
  • Nếu
    // Using slice, create newCar from myCar.
    const myHonda = { color: 'red', wheels: 4, engine: { cylinders: 4, size: 2.2 } };
    const myCar = [myHonda, 2, 'cherry condition', 'purchased 1997'];
    const newCar = myCar.slice(0, 2);
    
    console.log('myCar =', myCar);
    console.log('newCar =', newCar);
    console.log('myCar[0].color =', myCar[0].color);
    console.log('newCar[0].color =', newCar[0].color);
    
    // Change the color of myHonda.
    myHonda.color = 'purple';
    console.log('The new color of my Honda is', myHonda.color);
    
    console.log('myCar[0].color =', myCar[0].color);
    console.log('newCar[0].color =', newCar[0].color);
    
    7, không có gì được trích xuất.
const fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'];
const citrus = fruits.slice(1, 3);

// fruits contains ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
// citrus contains ['Orange','Lemon']
7 Tùy chọnOptional

Chỉ số dựa trên không tại đó để kết thúc trích xuất, được chuyển đổi thành một số nguyên. slice() trích xuất đến nhưng không bao gồm

const fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'];
const citrus = fruits.slice(1, 3);

// fruits contains ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
// citrus contains ['Orange','Lemon']
7.

  • Chỉ số âm tính trở lại từ cuối mảng - nếu
    myCar = [
      { color: 'red', wheels: 4, engine: { cylinders: 4, size: 2.2 } },
      2,
      'cherry condition',
      'purchased 1997'
    ]
    newCar = [ { color: 'red', wheels: 4, engine: { cylinders: 4, size: 2.2 } }, 2 ]
    myCar[0].color = red
    newCar[0].color = red
    The new color of my Honda is purple
    myCar[0].color = purple
    newCar[0].color = purple
    
    1,
    myCar = [
      { color: 'red', wheels: 4, engine: { cylinders: 4, size: 2.2 } },
      2,
      'cherry condition',
      'purchased 1997'
    ]
    newCar = [ { color: 'red', wheels: 4, engine: { cylinders: 4, size: 2.2 } }, 2 ]
    myCar[0].color = red
    newCar[0].color = red
    The new color of my Honda is purple
    myCar[0].color = purple
    newCar[0].color = purple
    
    2 được sử dụng.
  • Nếu
    myCar = [
      { color: 'red', wheels: 4, engine: { cylinders: 4, size: 2.2 } },
      2,
      'cherry condition',
      'purchased 1997'
    ]
    newCar = [ { color: 'red', wheels: 4, engine: { cylinders: 4, size: 2.2 } }, 2 ]
    myCar[0].color = red
    newCar[0].color = red
    The new color of my Honda is purple
    myCar[0].color = purple
    newCar[0].color = purple
    
    3,
    // Using slice, create newCar from myCar.
    const myHonda = { color: 'red', wheels: 4, engine: { cylinders: 4, size: 2.2 } };
    const myCar = [myHonda, 2, 'cherry condition', 'purchased 1997'];
    const newCar = myCar.slice(0, 2);
    
    console.log('myCar =', myCar);
    console.log('newCar =', newCar);
    console.log('myCar[0].color =', myCar[0].color);
    console.log('newCar[0].color =', newCar[0].color);
    
    // Change the color of myHonda.
    myHonda.color = 'purple';
    console.log('The new color of my Honda is', myHonda.color);
    
    console.log('myCar[0].color =', myCar[0].color);
    console.log('newCar[0].color =', newCar[0].color);
    
    6 được sử dụng.
  • Nếu
    myCar = [
      { color: 'red', wheels: 4, engine: { cylinders: 4, size: 2.2 } },
      2,
      'cherry condition',
      'purchased 1997'
    ]
    newCar = [ { color: 'red', wheels: 4, engine: { cylinders: 4, size: 2.2 } }, 2 ]
    myCar[0].color = red
    newCar[0].color = red
    The new color of my Honda is purple
    myCar[0].color = purple
    newCar[0].color = purple
    
    5 hoặc
    const fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'];
    const citrus = fruits.slice(1, 3);
    
    // fruits contains ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
    // citrus contains ['Orange','Lemon']
    
    7 bị bỏ qua,
    myCar = [
      { color: 'red', wheels: 4, engine: { cylinders: 4, size: 2.2 } },
      2,
      'cherry condition',
      'purchased 1997'
    ]
    newCar = [ { color: 'red', wheels: 4, engine: { cylinders: 4, size: 2.2 } }, 2 ]
    myCar[0].color = red
    newCar[0].color = red
    The new color of my Honda is purple
    myCar[0].color = purple
    newCar[0].color = purple
    
    7 được sử dụng, gây ra tất cả các yếu tố cho đến khi kết thúc được trích xuất.
  • Nếu
    const fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'];
    const citrus = fruits.slice(1, 3);
    
    // fruits contains ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
    // citrus contains ['Orange','Lemon']
    
    7 được định vị trước hoặc tại
    const fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'];
    const citrus = fruits.slice(1, 3);
    
    // fruits contains ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
    // citrus contains ['Orange','Lemon']
    
    6 sau khi bình thường hóa, không có gì được trích xuất.

Giá trị trả về

Một mảng mới chứa các yếu tố trích xuất.

Sự mô tả

Phương pháp slice() là một phương thức sao chép. Nó không thay đổi

const arrayLike = {
  length: 3,
  0: 2,
  1: 3,
  2: 4,
};
console.log(Array.prototype.slice.call(arrayLike, 1, 3));
// [ 3, 4 ]
1 mà thay vào đó trả về một bản sao nông chứa một số phần tử giống như các phần tử từ mảng gốc.

Phương pháp slice() bảo tồn các khe trống. Nếu phần cắt lát thưa thớt, mảng trả về cũng thưa thớt.

Phương pháp slice() là chung chung. Nó chỉ mong đợi giá trị

const arrayLike = {
  length: 3,
  0: 2,
  1: 3,
  2: 4,
};
console.log(Array.prototype.slice.call(arrayLike, 1, 3));
// [ 3, 4 ]
1 có thuộc tính
const arrayLike = {
  length: 3,
  0: 2,
  1: 3,
  2: 4,
};
console.log(Array.prototype.slice.call(arrayLike, 1, 3));
// [ 3, 4 ]
5 và các thuộc tính được khóa.

Ví dụ

Trả về một phần của một mảng hiện có

const fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'];
const citrus = fruits.slice(1, 3);

// fruits contains ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
// citrus contains ['Orange','Lemon']

Sử dụng lát cắt

Trong ví dụ sau,

const arrayLike = {
  length: 3,
  0: 2,
  1: 3,
  2: 4,
};
console.log(Array.prototype.slice.call(arrayLike, 1, 3));
// [ 3, 4 ]
6 tạo ra một mảng mới,
const arrayLike = {
  length: 3,
  0: 2,
  1: 3,
  2: 4,
};
console.log(Array.prototype.slice.call(arrayLike, 1, 3));
// [ 3, 4 ]
7, từ
const arrayLike = {
  length: 3,
  0: 2,
  1: 3,
  2: 4,
};
console.log(Array.prototype.slice.call(arrayLike, 1, 3));
// [ 3, 4 ]
8. Cả hai đều bao gồm một tham chiếu đến đối tượng
const arrayLike = {
  length: 3,
  0: 2,
  1: 3,
  2: 4,
};
console.log(Array.prototype.slice.call(arrayLike, 1, 3));
// [ 3, 4 ]
9. Khi màu của
const arrayLike = {
  length: 3,
  0: 2,
  1: 3,
  2: 4,
};
console.log(Array.prototype.slice.call(arrayLike, 1, 3));
// [ 3, 4 ]
9 được thay đổi thành màu tím, cả hai mảng đều phản ánh sự thay đổi.

// Using slice, create newCar from myCar.
const myHonda = { color: 'red', wheels: 4, engine: { cylinders: 4, size: 2.2 } };
const myCar = [myHonda, 2, 'cherry condition', 'purchased 1997'];
const newCar = myCar.slice(0, 2);

console.log('myCar =', myCar);
console.log('newCar =', newCar);
console.log('myCar[0].color =', myCar[0].color);
console.log('newCar[0].color =', newCar[0].color);

// Change the color of myHonda.
myHonda.color = 'purple';
console.log('The new color of my Honda is', myHonda.color);

console.log('myCar[0].color =', myCar[0].color);
console.log('newCar[0].color =', newCar[0].color);

Kịch bản này viết:

myCar = [
  { color: 'red', wheels: 4, engine: { cylinders: 4, size: 2.2 } },
  2,
  'cherry condition',
  'purchased 1997'
]
newCar = [ { color: 'red', wheels: 4, engine: { cylinders: 4, size: 2.2 } }, 2 ]
myCar[0].color = red
newCar[0].color = red
The new color of my Honda is purple
myCar[0].color = purple
newCar[0].color = purple

Gọi lát () trên các đối tượng không phải là

Phương pháp slice() đọc thuộc tính

const arrayLike = {
  length: 3,
  0: 2,
  1: 3,
  2: 4,
};
console.log(Array.prototype.slice.call(arrayLike, 1, 3));
// [ 3, 4 ]
5 của
const arrayLike = {
  length: 3,
  0: 2,
  1: 3,
  2: 4,
};
console.log(Array.prototype.slice.call(arrayLike, 1, 3));
// [ 3, 4 ]
1. Sau đó, nó đọc các thuộc tính được khóa nguyên từ
const fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'];
const citrus = fruits.slice(1, 3);

// fruits contains ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
// citrus contains ['Orange','Lemon']
6 đến
const fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'];
const citrus = fruits.slice(1, 3);

// fruits contains ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
// citrus contains ['Orange','Lemon']
7 và xác định chúng trên một mảng mới được tạo.

const arrayLike = {
  length: 3,
  0: 2,
  1: 3,
  2: 4,
};
console.log(Array.prototype.slice.call(arrayLike, 1, 3));
// [ 3, 4 ]

Sử dụng Slice () để chuyển đổi các đối tượng giống như mảng thành mảng

Phương pháp slice() thường được sử dụng với

// slice() is called with `this` passed as the first argument
const slice = Function.prototype.call.bind(Array.prototype.slice);

function list() {
  return slice(arguments);
}

const list1 = list(1, 2, 3); // [1, 2, 3]
7 và
// slice() is called with `this` passed as the first argument
const slice = Function.prototype.call.bind(Array.prototype.slice);

function list() {
  return slice(arguments);
}

const list1 = list(1, 2, 3); // [1, 2, 3]
8 để tạo phương thức tiện ích chuyển đổi một đối tượng giống như mảng thành một mảng.

// slice() is called with `this` passed as the first argument
const slice = Function.prototype.call.bind(Array.prototype.slice);

function list() {
  return slice(arguments);
}

const list1 = list(1, 2, 3); // [1, 2, 3]

Sử dụng Slice () trên các mảng thưa thớt

Mảng được trả về từ slice() có thể thưa thớt nếu nguồn thưa thớt.

console.log([1, 2, , 4, 5].slice(1, 4)); // [2, empty, 4]

Thông số kỹ thuật

Sự chỉ rõ
Thông số kỹ thuật ngôn ngữ Ecmascript # sec-array.prototype.slice
# sec-array.prototype.slice

Tính tương thích của trình duyệt web

Bảng BCD chỉ tải trong trình duyệt

Xem thêm

Bạn có thể cắt các đối tượng trong JavaScript không?

JavaScript Slice () Phương thức: Đối tượng mảng Phương thức Slice () được sử dụng để trích xuất một phần của một mảng.Phương thức trả về một mảng mới và nó không thay đổi mảng gốc, các phần tử được chọn của mảng gốc được sao chép thành một mảng mới.The slice() method is used to extract a section of an array. The method returns a new array and it does not change the original array, selected elements of the original array are copied into a new array.

Bạn có thể sử dụng lát cắt trên mảng không?

Phương thức Slice () có thể được sử dụng để tạo một bản sao của một mảng hoặc trả về một phần của một mảng.Điều quan trọng cần lưu ý là phương thức Slice () không làm thay đổi mảng gốc mà thay vào đó tạo ra một bản sao nông.. It is important to note that the slice() method does not alter the original array but instead creates a shallow copy.

Có thể cắt () được sử dụng trên chuỗi và mảng?

Các phương thức cắt lát () và splice () là cho các mảng.Phương thức chia () được sử dụng cho các chuỗi.. The split( ) method is used for strings.