New Array Method In Javascript
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toSpliced
https://www.youtube.com/watch?v=3CBD5JZJJKw
Array.prototype.with -->
//mutablity ---> changing the orignal one
const arr= ['Ankit','Tyagi','Teena','Anju']
arr.map((name,index)=>{
if(index===1) return 'Anand'
return name;
})
console.log(arr)
function then returns a new array with the results of the callback function.
Array.with(index,value)
Mutable --> We are changing the value of the Orignal Array or changing something
const nameArr =['Ankit', 'Teena',' Anju',' Anand'];
nameArr[1]='Someone';
console.log(nameArr)
const newArr =[...nameArr];
newArr[1]='Someone';
Performance Issue--> we are looping through the array when we do [...nameArr], we are again looping through the array newArr[1]='Someone'; when we assign the 2nd index some value
Immutable --> We will create a copy of the Orignal Array then we will update
const nameArr =['Ankit', 'Teena', 'Anju',' Anand'];
const updatedArray = nameArr.map((name,index)=>{
if(index===1){
return 'Someone'
}
return name
})
console.log('Orignal',nameArr)
console.log('UpdatedArray',updatedArray)
const updatedArrayUsingWith =nameArr.with(1,'Someone');
console.log('updatedArrayUsingWith',updatedArrayUsingWith)
Array.prototype.toSorted-->
Array.prototype.toReversed-->
Comments
Post a Comment