// 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);
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);
Array.prototype.insert = function(i,...rest){return this.slice(0,i).concat(rest,this.slice(i));}
var a = [3,4,8,9],b = a.insert(2,5,6,7);console.log(JSON.stringify(a));console.log(JSON.stringify(b));
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 itemnewItem,// 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]
这可用于通过稍微调整函数以对新项使用rest运算符来添加多个项,并将其扩展到返回的结果中:
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]
// Append at a 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 a specific position (here at index 3)arrName[3] = 'newName1';
在特定索引处附加多个元素
// Append from index number 1arrName.splice(1, 0, 'newElemenet1', 'newElemenet2', 'newElemenet3');// 1: index number from where append start,// 0: number of element to remove,//newElemenet1,2,3: new elements
let ifExist = (item, strings = [ '' ], position = 0) => {// Output into an array with an empty string. Important just in case their isn't any item.let output = [ '' ];// Check to see if the item that will be positioned exist.if (item) {// Output should be equal to an array of strings.output = strings;// Use splice() in order to break the array.// Use positional parameters to state where to put the item// and 0 is to not replace an index. Item is the actual item we are placing at the prescribed position.output.splice(position, 0, item);}// Empty string is so we do not concatenate with comma or anything else.return output.join("");};
var array= [10,20,30,40]
var i;
var pos=2; //pos=index + 1/*pos is position which we want to insert at which is index + 1.position two in an array is index 1.*/
var value=5//value to insert
//Initialize from last array element
for(i=array.length-1;i>=pos-1;i--){
array[i+1]=array[i]
}
array[pos-1]=value
console.log(array)
/*** @param arr: Array* @param item: item to insert* @param index: index at which to insert* @returns array with the inserted element*/export function _arrayInsertAt<T>(arr: T[], item: T, index: number) {return arr.splice(index, 0, item);;}