JSFiddle - React, Tailwind, and code Playground
JavaScript
let ob1 = {"name": "name1", "age": 10}
let ob2 = {"name": "name2", "age": 12}
let collection = [ob1, ob2]
console.log('initial collection')
console.log(JSON.stringify(collection))
const mapper = (ob, index) => ({
...ob,
'age': ob.age + index
})
const mutatingMapper = (ob, index) => {
ob.age = ob.age + index + 5
return ob
}
let mappedCollection = collection.map(mapper)
console.log('collection after mapping:')
console.log(JSON.stringify(collection))
console.log('mappedCollection:')
console.log(JSON.stringify(mappedCollection))
console.log('ob1 after mapping')
console.log(JSON.stringify(ob1))
console.log('ob2 after mapping')
console.log(JSON.stringify(ob2))
let mutatedCollection = collection.map(mutatingMapper)
console.log('collection after mapping with mutation: ')
console.log(JSON.stringify(collection))
console.log('mutatedCollection after mapping with mutation: ')
console.log(JSON.stringify(mutatedCollection))
console.log('mappedCollection after mapping with mutation: ')
console.log(JSON.stringify(mappedCollection))
console.log('ob1 after mutatingMapping')
console.log(JSON.stringify(ob1))
console.log('ob2 after mutatingMapping')
console.log(JSON.stringify(ob2))