[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/slice [Back]  [Original]

Array.prototype.slice() - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

Array.prototype.slice()

Baseline

20157

slice() Array start end end start end

const animals = ["ant", "bison", "camel", "duck", "elephant"];

console.log(animals.slice(2));
// : Array ["camel", "duck", "elephant"]

console.log(animals.slice(2, 4));
// : Array ["camel", "duck"]

console.log(animals.slice(1, 5));
// : Array ["bison", "camel", "duck", "elephant"]

console.log(animals.slice(-2));
// : Array ["duck", "elephant"]

console.log(animals.slice(2, -1));
// : Array ["camel", "duck"]

console.log(animals.slice());
// : Array ["ant", "bison", "camel", "duck", "elephant"]

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

start

  • -array.length <= start < 0 start + array.length
  • start < -array.length start 0
  • start >= array.length
end

slice() end

  • -array.length <= end < 0 end + array.length
  • end < -array.length 0
  • end >= array.length end array.length
  • end start

slice() this

slice()

slice() this length

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

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

slice(1, 3) 1 3 3 ['Orange', 'Lemon']

end

js
const fruits = ["Apple", "Banana", "Orange", "Mango", "Pineapple"];

const tropical = fruits.slice(2);
console.log(tropical); // ['Orange', 'Mango', 'Pineapple']

slice(2) 2

js
const fruits = ["Apple", "Banana", "Orange", "Mango", "Pineapple"];

const lastTwo = fruits.slice(-2);
console.log(lastTwo); // ['Mango', 'Pineapple']

slice(-2) 2 slice -12 -2 -2

|     |     |     |     |     |
|  S  |  L  |  I  |  C  |  E  |
|     |     |     |     |     |
  -5    -4    -3    -2    -1

<--- 

js
const fruits = ["Apple", "Banana", "Orange", "Mango", "Pineapple"];

// 
const sliceExample = fruits.slice(1, -1);
console.log(sliceExample); // ['Banana', 'Orange', 'Mango']

slice(1, -1) 1 -1 () ['Banana', 'Orange', 'Mango'] slice

 --->

   0     1     2     3     4
|     |     |     |     |     |
|  S  |  L  |  I  |  C  |  E  |
|     |     |     |     |     |
  -5    -4    -3    -2    -1

<--- 

slice

slice newCar myCar myHonda myHonda purple

js
// slice myCar  newCar 
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);

// 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);

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

slice()

slice() this length start end

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

slice()

slice() bind() call()

js
// slice()  1  `this` 
const slice = Function.prototype.call.bind(Array.prototype.slice);

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

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

slice()

slice()

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

ECMAScript 2027 LanguageSpecification
# sec-array.prototype.slice


Web Proxy Viewer  |  New URL  |  Original Page