# 返回新数组，不改变原数组

## join

``````// 0.0.2/join.js
const arr = ['pr', 'is', 18];

console.log(arr.join(' ')); // pr is 18
console.log('=>');
console.log(arr); // [ 'pr', 'is', 18 ]

## concat

``````// 0.0.2/concat.js
const arr = [1, 2, 3, 4];
const arr1 = ['pr', 'is', 'a', 'boy'];
const arr2 = [5, 6, 7];

console.log(arr.concat(arr1, arr2).concat(8, 9)); // [1, 2, 3, 4, 'pr', 'is', 'a', 'boy', 5, 6, 7, 8, 9 ]
console.log('=>');
console.log(arr); // [ 1, 2, 3, 4 ]

## slice

``````// 0.0.2/slice.js
const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

console.log(arr.slice(1, 5)); // [ 1, 2, 3, 4 ]
console.log('=>');
console.log(arr); // [ 0,1,2,3,4,5,6,7,8,9 ]

`slice(1, 5)` 可见里面最多含 4（`5 - 1`） 个元素，而且从第 1 位开始取。

## map

``````// 0.0.2/map.js
const arr = [1, 2, 3, 4];

console.log(arr.map(i => i * 10 - 5)); // [ 5, 15, 25, 35 ]
console.log('=>');
console.log(arr); // [ 1, 2, 3, 4 ]

## every

• 如果数组中检测到有一个元素不满足，则整个表达式返回 `false`，且剩余的元素不会再进行检测；
• 如果所有元素都满足条件，则返回 `true`
``````// 0.0.2/every.js
const arr = [1, 2, 3, 4];

console.log(arr.every(i => i > 2)); // false
console.log(arr.every(i => i > 0)); // true
console.log([].every(i => i === 'pr')); // true
console.log('=>');
console.log(arr); // [ 1, 2, 3, 4 ]

## some

• 如果有一个元素满足条件，则表达式返回 `true`, 剩余的元素不会再执行检测;
• 如果没有满足条件的元素，则返回 `false`;
``````// 0.0.2/some.js
const arr = [1, 2, 3, 4];

console.log(arr.some(i => i > 4)); // false
console.log(arr.some(i => i > 0)); // true
console.log([].some(i => i === 'pr')); // false
console.log('=>');
console.log(arr); // [ 1, 2, 3, 4 ]

## filter

``````// 0.0.2/filter.js
const arr = [1, 2, 3, 4];

console.log(arr.filter(i => i > 2)); // [3, 4]
console.log([].filter(i => i === 'pr')); // []
console.log('=>');
console.log(arr); // [ 1, 2, 3, 4 ]

## forEach

``````// 0.0.2/forEach.js
const arr = [1, 2, 3, 4];
const copy = [];

console.log(arr.forEach(i => {
copy.push(i * 2);
}));
console.log(copy); // [ 2, 4, 6, 8 ]
console.log('=>');
console.log(arr); // [ 1, 2, 3, 4 ]

## reduce

``````// 0.0.2/reduce.js
const arr = [1, 2, 3, 4];

console.log(arr.reduce((prev, cur) => prev + cur, 0)); // 10
console.log('=>');
console.log(arr); // [ 1, 2, 3, 4 ]

# 返回新数组，改变原数组

## pop

• 如果数组为空，则不改变数组，返 `undefined`
``````// 0.0.2/pop.js
const arr = [1, 2, 3, 4];
const arr1 = [];

console.log(arr.pop()); // 4
console.log(arr1.pop()); // undefined
console.log('=>');
console.log(arr); // [ 1, 2, 3 ]
console.log(arr1); // []

## push

``````// 0.0.2/push.js
const arr = [1, 2, 3, 4];

console.log(arr.push(5)); // 5
console.log(arr.push([1, 2])); // 6
console.log('=>');
console.log(arr); // [ 1, 2, 3, 4, 5, [ 1, 2 ] ]

## shift

``````// 0.0.2/shift.js
const arr = [1, 2, 3, 4];

console.log(arr.shift()); // 1
console.log('=>');
console.log(arr); // [ 2, 3, 4 ]

## unshift

``````// 0.0.2/unshift.js
const arr = [1, 2, 3, 4];

console.log(arr.unshift(5, 6)); // 6
console.log(arr.unshift([1, 2])); // 7
console.log('=>');
console.log(arr); // [ [ 1, 2 ], 5, 6, 1, 2, 3, 4 ]

## reverse

``````// 0.0.2/reverse.js
const arr = [1, 2, 3, 4];
const hello = 'hello';
const helloArray = hello.split('');

console.log(helloArray.reverse().join('')); // olleh
console.log(arr.reverse()); // [ 4, 3, 2, 1 ]
console.log('=>');
console.log(arr); // [ 4, 3, 2, 1 ]
console.log(helloArray); // [ 'o', 'l', 'l', 'e', 'h' ]

## sort

``````// 0.0.2/sort.js
const arr = [1, 2, 3, 4, 10, 12, 22];

console.log(arr.sort()); // [ 1, 10, 12, 2, 22, 3, 4];
console.log('=>');
console.log(arr); // [ 1, 10, 12, 2, 22, 3, 4];

## splice

``````// 0.0.2/splice.js
const arr = [1, 2, 3, 4];

console.log(arr.splice(1, 2, 10, 12)); // [ 2, 3 ]
console.log('=>');
console.log(arr); // [ 1, 10, 12, 4 ]