I have two arrays that I need to check the difference upon and return the index of that difference.
For example, I currently have two arrays that get updated when the input's value is changed. The newTags
array gets updated whenever there is a new tag within the input, such as @testing
. I need to compare the newTags
array with the oldTags
array and return the index of the difference.
I am currently stringifying both arrays and comparing them that way, although it is unable to return the index of the difference.
var newTags = [];
var oldTags = [];
$input.on('keyup', function () {
var newValue = $input.val();
var pattern = /@[a-zA-Z]+/ig;
var valueSearch = newValue.search(pattern);
if (valueSearch >= 0) {
newTags = newValue.match(pattern);
if ((newTags + "") != (oldTags + "")) {
//Need index of difference here
console.log(newTags, oldTags);
}
oldTags = newTags;
}
});
["@testing", "@hello"]
and oldTags could be["@test", "@hello"]
. Therefore the index of the difference is 0 since the first value in the array is different. – PinzlernewTags
witholdTags
and return the position in the array of the difference. – Pinzler$.each
loop may be the best solution... – Pinzler