I'm having a problem with removing an element from a certain array, but it removes the element from BOTH that array and another array that's irrelevant. Does anyone spot the problem, because to me it looks correct and should remove the element, "Beer" from the "nearby.items" array and NOT the "locations_dictionary[0].items" array.
var nearby = {
"looted": false,
"items": ["Apple","Rusted Sword","Beer","Bronze Sword","Wooden Sword","Excalibur","Bronze Coin"]
};
const locations_dictionary = [
{
"name": "City",
"items": ["Apple","Rusted Sword","Beer","Bronze Sword","Wooden Sword","Excalibur","Bronze Coin"]
},
{
"name": "Forest",
"items": ["Apple","Stick","Rusted Sword","Corpse","Rusted Dagger"]
}
];
function Remove_Item_From_Array(a,v) { //a = array, v = value
for(i = 0; i < a.length; i++) {
if(a[i] === v) {
a.splice(i,1);
}
}
}
nearby.items.forEach(function(value,index) {
if("Beer" == value) {
Remove_Item_From_Array(nearby.items,"Beer");
return;
}
})
console.log('nearby array ---> ' , nearby, '\n locations ---> ', locations_dictionary)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>