When I query my DB for items i generate an array say a list like
ArrayOne = [Apple, Banana, Orange, Banana]
//yes, an item can be there twice or more
then I duplicate it in a ArrayOne_Copy = ArrayOne
ok, then after an interval of 5 minutes I check again the DB and generate and replace the array ArrayOne
say now is like
ArrayOne = [Apple, Peach, Banana, Pineapple, Banana, Melon]
at this point I want to compare the ArrayOne_Copy
with the newly generated ArrayOne
and rebuild the list from the new list but without clearing the list and generating a new output, but i want to only add the new items or remove the one no longer in the list
if I can "speak" code I would tell the script to:
Don't touch `Apple` because has not changed
Push 'Peach" between `Apple` and `Banana`
Push `Pineapple` between `Banana` and `Banana`
Remove `Orange`
Add `Melon`
so. I'm kinda lost, and maybe the solution is very simple. Can you suggest the best practice?
You're not duplicating it, you're referencing it.
You will need to make a hard copy of the array which can be done using slice()
.
var ArrayOne_Copy = ArrayOne.slice(0);
For example:
var ArrayOne = ['Apple', 'Banana', 'Orange', 'Banana'];
var ArrayOne_Copy = ArrayOne.slice(0);
ArrayOne.push('Melon');
console.log(ArrayOne);
console.log(ArrayOne_Copy);
You can use this kind of function:
Array.prototype.complete = function( arr ){
var result = new Array();
for( var a in arr )
result[ a ] = this[ a ] ? this[ a ] : arr[ a ];
return result;
}
Like this :
ArrayFull = ArrayOne_Copy.complete( ArrayOne );
Then ArrayFull
will be the array modified with .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.