| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/slice | [Back] [Original] |
Get to know MDN better
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"]
slice()
slice(start)
slice(start, end)
start -array.length <= start < 0 start + array.length start < -array.length start 0 start >= array.length end -array.length <= end < 0 end + array.length end < -array.length 0 end >= array.length end array.length end start 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']
const fruits = ["Apple", "Banana", "Orange", "Mango", "Pineapple"];
const tropical = fruits.slice(2);
console.log(tropical); // ['Orange', 'Mango', 'Pineapple']
slice(2) 2
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 <---
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 newCar myCar myHonda myHonda purple
// 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() this length start end
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() 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()
console.log([1, 2, , 4, 5].slice(1, 4)); // [2, empty, 4]
| ECMAScript 2027 LanguageSpecification # sec-array.prototype.slice |
Arrayat()concat()copyWithin()entries()every()fill()filter()find()findIndex()findLast()findLastIndex()flat()flatMap()forEach()includes()indexOf()join()keys()lastIndexOf()map()pop()push()reduce()reduceRight()reverse()shift()slice()some()sort()splice()toLocaleString()toReversed()toSorted()toSpliced()toString()unshift()values()with()[Symbol.iterator]()Object/Function| Web Proxy Viewer | New URL | Original Page |