有没有办法在Javascript中检索最近添加/插入数组的元素?
为了清楚起见,我不是指数组中的 last 元素,我的意思是最后插入的元素到某个任意位置。< / p>
如果在没有明确存储插入时的最后一个索引的情况下无法做到这一点,那么我将基于此寻找最简单的答案。
对于那些询问元素添加方式的人,请说我已经完成了这个:
// Insert new_element at index, shift other elements to accomodate it
existing_array.splice(index, 0, new_element);
答案 0 :(得分:1)
没有办法。
您必须跟踪最后插入的位置。
这是一个简单的代码示例:
Array.prototype.lastIndex = -1;
// edit that to match your actual insertion method
function insertInArray(arr, val, index) {
arr[index] = val;
arr.lastIndex = index;
}
var a = [1, 2, 3];
var b = [4, 5, 6];
console.log("a before: " + a.lastIndex);
console.log("b before: " + b.lastIndex);
insertInArray(a, 1, 2);
console.log("a after: " + a.lastIndex);
console.log("b after: " + b.lastIndex);
&#13;
答案 1 :(得分:1)
从理论上讲,您可以扩展Array原型以获取此信息:
Array.prototype.lastAdded = {
index: -1,
value: null
};
Array.prototype.add = function (element, index) {
//If no index, put at back
if (index === void 0) {
index = this.length;
}
//Add value
this[index] = element;
//Set "lastAdded";
this.lastAdded = {
index: index,
value: this[index]
};
}
var list = [1, 2, 3, 4, 5, 6, 7, 8, 9];
list.add(10);
console.log(list.lastAdded);
list.add(2, 2);
console.log(list.lastAdded);
编辑1 - 使用包装器对象
以下是您离开Native Objects
的例子:
console.clear();
var ArrayWrapper = (function() {
function ArrayWrapper(array) {
if (array === void 0) {
array = [];
}
this.array = array;
var self = this;
for (var key in Object.getOwnPropertyNames(Array)) {
var element = Object.getOwnPropertyNames(Array)[key];
(function(el) {
self[el] = function() {
//make a clone
var oldArr = JSON.parse(JSON.stringify(self.array));
self.array[el].apply(self.array, arguments);
//If a change has occured
var change = false;
for (var key in self.array) {
if (self.array.hasOwnProperty(key)) {
if (!oldArr.hasOwnProperty(key)) {
//New key
self.lastChange = key;
change = true;
break;
} else if (oldArr[key] != self.array[key]) {
//Modified key
self.lastChange = key;
change = true;
break;
}
}
}
//If it wasn't added or modifed we test for deleted
if (!change) {
for (var key in oldArr) {
if (oldArr.hasOwnProperty(key)) {
if (!self.array.hasOwnProperty(key)) {
//deleted key
self.lastChange = key;
break;
}
}
}
}
};
})(element);
}
}
return ArrayWrapper;
}());
var xarray = new ArrayWrapper([1, 2, 3]);
console.log("current array", xarray.array);
xarray.push(1232);
console.log("change at", xarray.lastChange);
console.log("current array", xarray.array);
xarray.splice(0, 1, 8);
console.log("change at", xarray.lastChange);
console.log("current array", xarray.array);
当我说它会更复杂的时候,我不是在开玩笑。
此方法允许您在Array
的本机函数之间创建一个图层,而不实际触及所述函数。这样本机对象保持不变。
这可用于存储可重用的数组函数(如非常独特的排序或过滤方法),或者在本例中,用于包含有关它所包含的数组的元数据。
答案 2 :(得分:0)
您可以使用ES6 Proxies
执行此操作请参阅下面的代码段
var arrayChangeHandler = {
lastChangedIndex :undefined,
get: function(target, property) {
return (property == "lastChangedIndex") ? this.lastChangedIndex :target[property];
},
set: function(target, property, value, receiver) {
target[property] = value;
(property!=="length") && (this.lastChangedIndex = property);
return true;
}
};
var myArray = new Proxy([1,2,3,4,5,6,7,8,9], arrayChangeHandler);
myArray[5] = 0;
myArray[7] = 3;
console.log(myArray.lastChangedIndex) ;
myArray[1] = 2;
myArray[4] = 4;
console.log(myArray.lastChangedIndex) ;
myArray.push(10);
myArray.push(11);
myArray.push(14);
console.log(myArray.lastChangedIndex) ;
myArray.splice(9,0,22);
console.log(myArray.lastChangedIndex) ;
&#13;
答案 3 :(得分:0)
var existing_array = [50, 51, 52, 53, 54];
var origin_splice = existing_array.splice;
existing_array.splice = function splice(index) {
origin_splice.apply(this, [].slice.call(arguments));
this.splice_lastIndex = index;
};
existing_array.splice(3, 0, "ok");
console.log(existing_array); // Array [ 50, 51, 52, "ok", 53, 54 ]
console.log(existing_array.splice_lastIndex); // 3
&#13;