I am trying to implement some logic inn which i would like to have the array as it is, but when i tried to do with forEach and map the values in info array get mutated.
var info = [{"date":"today","location":["a","b","c","d","e","f","g","h"]},{"date":"yesterday","location":["a","b","c"]},{"date":"tomorrow","location":["a","b","c"]}]
var showItems = [];
var restrictItems= [];
info.forEach(res =>{
if(res.location.length && restrictItems.length<5)
{
let slicedLocation = res.location.slice(0,5-restrictItems.length);
slicedLocation.map((item, i) => {
restrictItems.push(item);
});
res.location = slicedLocation;
showItems.push(res);
}
})
console.log("showItemsshowItems",showItems);// here i am getting the output as [{"date":"today","location":["a","b","c","d","e"]}] which is expected
console.log("info",info)// here i am not getting the original value of info array(f,g,h) is missing in First array.
[{"date":"today","location":["a","b","c","d","e"]},{"date":"yesterday","location":["a","b","c"]},{"date":"tomorrow","location":["a","b","c"]}]
Please help here, thanks in advance.