CustomArray.prototype.erase = function() {
var target, args = arguments,
length = args.length,
index;
while (length && this.length) {
target = args[--length];
while ((index = this.indexOf(target)) !== -1) {
this.splice(index, 1);
}
}
return this;
};
var data = [{
title: 'Bookable',
start: moment("2018-04-05 06:00"),
end: moment("2018-04-05 07:00"),
allDay: false
}, {
title: 'Bookable',
start: moment("2018-04-05 06:00"),
end: moment("2018-04-05 07:00"),
allDay: false
}, {
title: 'Bookable',
start: moment("2018-04-05 06:00"),
end: moment("2018-04-05 07:00"),
allDay: false
}, {
title: 'Bookable',
start: moment("2018-04-05 06:00"),
end: moment("2018-04-05 07:00"),
allDay: false
}]
var targetsToRemove = [{
title: 'Bookable',
start: moment("2018-04-06 06:00"),
end: moment("2018-04-06 07:00"),
allDay: false
}];
console.log("Before: " + data.length)
for (var i = 0; i < targetsToRemove.length; i++) {
data.erase(moment(targetsToRemove[i].start).format("YYYY-MM-DD HH:mm"));
}
console.log("After: " + data.length)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.0/moment.js"></script>
I aim to eliminate certain elements from one array by using another as a guide. Both arrays consist solely of objects similar to the one below. All corresponding objects in targetsToRemove
should be deleted from the data
array. The start
property seems suitable for this purpose, but there is no Id
.
The object structure is:
var item = {
title: 'Bookable',
start: moment(datesValue + " " + hourValue.start),
end: moment(datesValue + " " + hourValue.end),
allDay: false
};
Prototype.remove
CustomArray.prototype.erase = function () {
var target, args = arguments,
length = args.length,
index;
while (length && this.length) {
target = args[--length];
while ((index = this.indexOf(target)) !== -1) {
this.splice(index, 1);
}
}
return this;
};
Usage example:
for (var i = 0; i < targetsToRemove.length; i++) {
data.erase(moment(targetsToRemove[i].start).format("YYYY-MM-DD HH:mm"));
}
An error
Cannot read property 'indexOf' of undefined
occurs with this code, causing all objects to be removed. An unsuccessful demonstration is also provided. Any suggestions on improving this removal process are welcomed.
Your assistance is greatly appreciated.