Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript:检索最近插入的数组元素_Javascript_Arrays - Fatal编程技术网

Javascript:检索最近插入的数组元素

Javascript:检索最近插入的数组元素,javascript,arrays,Javascript,Arrays,有没有办法检索最近添加/插入到Javascript数组中的元素 非常清楚,我不是指数组中的最后一个元素,我是指最后插入到任意位置的元素 如果在插入时没有显式存储最后一个索引的方法,那么我将在此基础上寻找最简单的答案 对于那些询问如何添加元素的人,假设我已经这样做了: // Insert new_element at index, shift other elements to accomodate it existing_array.splice(index, 0, new_element);

有没有办法检索最近添加/插入到Javascript数组中的元素

非常清楚,我不是指数组中的最后一个元素,我是指最后插入到任意位置的元素

如果在插入时没有显式存储最后一个索引的方法,那么我将在此基础上寻找最简单的答案

对于那些询问如何添加元素的人,假设我已经这样做了:

// Insert new_element at index, shift other elements to accomodate it
existing_array.splice(index, 0, new_element);
没有办法

您必须跟踪上次插入的位置

下面是一个简单的代码示例:

Array.prototype.lastIndex=-1;
//编辑它以匹配实际的插入方法
函数插入数组(arr、val、索引){
arr[index]=val;
arr.lastIndex=索引;
}
VarA=[1,2,3];
VarB=[4,5,6];
console.log(“a before:+a.lastIndex”);
console.log(“b before:+b.lastIndex”);
插入线(a,1,2);
console.log(“a在:+a.lastIndex之后);
console.log(“b在:+b.lastIndex之后”)没有办法

您必须跟踪上次插入的位置

下面是一个简单的代码示例:

Array.prototype.lastIndex=-1;
//编辑它以匹配实际的插入方法
函数插入数组(arr、val、索引){
arr[index]=val;
arr.lastIndex=索引;
}
VarA=[1,2,3];
VarB=[4,5,6];
console.log(“a before:+a.lastIndex”);
console.log(“b before:+b.lastIndex”);
插入线(a,1,2);
console.log(“a在:+a.lastIndex之后);

console.log(“b在:+b.lastIndex之后”)理论上,您可以扩展阵列原型以提供以下信息:

Array.prototype.lastAdded={
索引:-1,
值:null
};
Array.prototype.add=函数(元素,索引){
//如果没有索引,请放在后面
如果(索引===无效0){
索引=此长度;
}
//增值
此[索引]=元素;
//设置“最后添加”;
this.lastAdded={
索引:索引,,
值:此[索引]
};
}
var list=[1,2,3,4,5,6,7,8,9];
增加(10);
console.log(list.lastAdded);
增加(2,2);

console.log(list.lastAdded)理论上,您可以扩展阵列原型以提供以下信息:

Array.prototype.lastAdded={
索引:-1,
值:null
};
Array.prototype.add=函数(元素,索引){
//如果没有索引,请放在后面
如果(索引===无效0){
索引=此长度;
}
//增值
此[索引]=元素;
//设置“最后添加”;
this.lastAdded={
索引:索引,,
值:此[索引]
};
}
var list=[1,2,3,4,5,6,7,8,9];
增加(10);
console.log(list.lastAdded);
增加(2,2);
console.log(list.lastAdded)您可以使用

请参阅下面的代码片段

var arrayChangeHandler={
lastChangedIndex:未定义,
get:函数(目标、属性){
return(property==“lastChangedIndex”)?this.lastChangedIndex:target[property];
},
集合:函数(目标、属性、值、接收者){
目标[财产]=价值;
(property!=“length”)&&(this.lastChangedIndex=property);
返回true;
}
};
var myArray=新代理([1,2,3,4,5,6,7,8,9],arrayChangeHandler);
myArray[5]=0;
myArray[7]=3;
log(myArray.lastChangedIndex);
myArray[1]=2;
myArray[4]=4;
log(myArray.lastChangedIndex);
myArray.push(10);
myArray.push(11);
myArray.push(14);
log(myArray.lastChangedIndex);
myArray.剪接(9,0,22);
log(myArray.lastChangedIndex)您可以使用

请参阅下面的代码片段

var arrayChangeHandler={
lastChangedIndex:未定义,
get:函数(目标、属性){
return(property==“lastChangedIndex”)?this.lastChangedIndex:target[property];
},
集合:函数(目标、属性、值、接收者){
目标[财产]=价值;
(property!=“length”)&&(this.lastChangedIndex=property);
返回true;
}
};
var myArray=新代理([1,2,3,4,5,6,7,8,9],arrayChangeHandler);
myArray[5]=0;
myArray[7]=3;
log(myArray.lastChangedIndex);
myArray[1]=2;
myArray[4]=4;
log(myArray.lastChangedIndex);
myArray.push(10);
myArray.push(11);
myArray.push(14);
log(myArray.lastChangedIndex);
myArray.剪接(9,0,22);
log(myArray.lastChangedIndex)
var现有_数组=[50,51,52,53,54];
var origin_splice=现有_array.splice;
现有_array.splice=功能拼接(索引){
origin_splice.apply(这个,[].slice.call(参数));
this.splice_lastIndex=索引;
};
现有_阵列拼接(3,0,“正常”);
console.log(现有_数组);//数组[50,51,52,“ok”,53,54]
console.log(现有的_数组.splice_lastIndex);//3
var现有_数组=[50,51,52,53,54];
var origin_splice=现有_array.splice;
现有_array.splice=功能拼接(索引){
origin_splice.apply(这个,[].slice.call(参数));
this.splice_lastIndex=索引;
};
现有_阵列拼接(3,0,“正常”);
console.log(现有_数组);//数组[50,51,52,“ok”,53,54]

console.log(现有的_数组.splice_lastIndex);//3
取决于如何插入阵列?您使用push还是arr[id]=element@Depzor您需要保存阵列的第二个最新版本的副本,并使其保持最新。此时,只跟踪上一次更新的值的索引会更快、更节省空间。我们编辑了问题,以包括一个如何插入元素的示例,并且再次强调,不,在数组中没有跟踪“最新添加的”,您必须自己跟踪。您可以使用ES 6代理来执行此操作,请看我的回答。如何插入数组?您使用push还是arr[id]=element@Depzor您需要保存阵列的第二个最新版本的副本,并使其保持最新。在这一点上,只跟踪上一次更新的索引会更快、更节省空间