0

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.

3 Answers 3

1

You should clone info variable.

var cloned = JSON.parse(JSON.stringify(info))

And use cloned variable to keep info.

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 cloned = JSON.parse(JSON.stringify(info))

var showItems = [];
var restrictItems= [];
cloned.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);
console.log("info",info)

Sign up to request clarification or add additional context in comments.

Comments

0

You can clone your original array to prevent mutation by using slice() method:

let clonedInfo = info.slice();

Comments

0

First item of array: you slice it (0,5) (it means abcde), and assign it to 'slicedLocation', then you assign res.location = slicedLocation;. That is why your 'location' of first item contain only abcde

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.