In my program, I am working on creating a new array based on two arrays named "ideaList" and "endorsements", which are declared globally. To ensure the immutability of ideaList and endorsements since they are used in other parts of the codebase, I have attempted to use .map and .filter methods.
function prepareIdeaArray(){
var preFilteredIdeas=ideaList
.filter(hasIdeaPassedControl)
.map(obj => {obj.count = endorsements
.filter(x=>x.ideaNumber===obj.ideaNumber)
.reduce((sum, x)=>sum+x.count,0);
obj.like = endorsements
.filter(x=>x.ideaNumber===obj.ideaNumber && x.who===activeUser)
.reduce((sum, x)=>sum+x.count,0)===0?false:true
obj.position = generatePosition(obj.status)
obj.description = obj.description.replace(/\n/g, '<br>')
return obj;});
preFilteredIdeas.sort(compareOn.bind(null,'count',false)).sort(compareOn.bind(null,'position',true))
return preFilteredIdeas;
}
Despite my efforts to maintain immutability using .map and .filter, upon checking ideaList using console.log after executing this function, I noticed that objects within the array had their "count", "like", and "position" properties mutated.
I also tried using .map alone without success in preventing mutation.
If you have any suggestions on how to prevent ideaList from being mutated while avoiding the use of const (since ideaList is initially declared globally before being assigned data in another function), I would greatly appreciate it.