# 自定义数组`insert`方法

### 1.有多个论点和链接支持

``````/* Syntax:
array.insert(index, value1, value2, ..., valueN) */

Array.prototype.insert = function(index) {
this.splice.apply(this, [index, 0].concat(
Array.prototype.slice.call(arguments, 1)));
return this;
};
``````

``````["a", "b", "c", "d"].insert(2, "X", "Y", "Z").slice(1, 6);
// ["b", "X", "Y", "Z", "c"]
``````

### 2.使用数组类型参数合并和链接支持

``````/* Syntax:
array.insert(index, value1, value2, ..., valueN) */

Array.prototype.insert = function(index) {
index = Math.min(index, this.length);
arguments.length > 1
&& this.splice.apply(this, [index, 0].concat([].pop.call(arguments)))
&& this.insert.apply(this, arguments);
return this;
};
``````

``````["a", "b", "c", "d"].insert(2, "V", ["W", "X", "Y"], "Z").join("-");
// "a-b-V-W-X-Y-Z-c-d"
``````

``````arr.insert(index, item)
``````

``````//Append at specific position(here at index 1)
arrName.splice(1, 0,'newName1');
//1: index number, 0: number of element to remove, newName1: new element

//Append at specific position (here at index 3)
arrName[3] = 'newName1';
``````

``````//Append from index number 1
arrName.splice(1, 0,'newElemenet1', 'newElemenet2', 'newElemenet3');
//1: index number from where append start,
//0: number of element to remove,
//newElemenet1,2,3: new elements
``````

`arr.splice(index, 0, item);``item`插入到指定索引的`arr`中（首先删除`0`项，也就是说，它只是一个插入）。

``````var arr = [];
arr[0] = "Jani";
arr[1] = "Hege";
arr[2] = "Stale";
arr[3] = "Kai Jim";
arr[4] = "Borge";

console.log(arr.join());
arr.splice(2, 0, "Lene");
console.log(arr.join());``````

``````var jsonb_str = '{"one": {"abbr": "", "order": 3}, "two": {"abbr": "", "order": 4}, "three": {"abbr": "", "order": 5}, "initialize": {"abbr": "init", "order": 1}, "start": {"abbr": "", "order": 2}}';

var jsonb_obj = JSON.parse(jsonb_str);
``````

``````var obj_length = Object.keys(jsonb_obj).length;
var sorted_array = new Array(obj_length);
``````

``````for (var key of Object.keys(jsonb_obj)) {
var tobj = {};
tobj[key] = jsonb_obj[key].abbr;

var position = jsonb_obj[key].order - 1;
sorted_array[position] = tobj;
}

console.dir(sorted_array);
``````

``````function insertAt(array, index) {
var arrayToInsert = Array.prototype.splice.apply(arguments, [2]);
return insertArrayAt(array, index, arrayToInsert);
}

function insertArrayAt(array, index, arrayToInsert) {
Array.prototype.splice.apply(array, [index, 0].concat(arrayToInsert));
return array;
}
``````

``````// if you want to insert specific values whether constants or variables:
insertAt(arr, 1, "x", "y", "z");

// OR if you have an array:
var arrToInsert = ["x", "y", "z"];
insertArrayAt(arr, 1, arrToInsert);
``````

``````Array.prototype.insert = function ( index, item ) {
this.splice( index, 0, item );
};
``````

``````var arr = [ 'A', 'B', 'D', 'E' ];
arr.insert(2, 'C');

// => arr == [ 'A', 'B', 'C', 'D', 'E' ]
``````

``````var initialArr = ["India","China","Japan","USA"];
initialArr.splice(index, 0, item);
``````

``````initialArr.splice(2, 0, "Nigeria");
initialArr.splice(2, 0, "Australia","UK");
``````

``````const items = [1, 2, 3, 4, 5]

const insert = (arr, index, newItem) => [
// part of the array before the specified index
...arr.slice(0, index),
// inserted item
newItem,
// part of the array after the specified index
...arr.slice(index)
]

const result = insert(items, 1, 10)

console.log(result)
// [1, 10, 2, 3, 4, 5]``````

``````const items = [1, 2, 3, 4, 5]

const insert = (arr, index, ...newItems) => [
// part of the array before the specified index
...arr.slice(0, index),
// inserted items
...newItems,
// part of the array after the specified index
...arr.slice(index)
]

const result = insert(items, 1, 10, 20)

console.log(result)
// [1, 10, 20, 2, 3, 4, 5]``````