首页 > 其他分享 >[ES2024] Simplify array immutable changes with the new array.with method

[ES2024] Simplify array immutable changes with the new array.with method

时间:2024-05-22 14:32:23浏览次数:12  
标签:idx completed ES2024 id new array method todos

The new Array.with method gives you an immutable syntax for changing values of an array at a specified index.

Sometimes .map will be more efficient. So, in this lesson we'll compare both methods while replacing an object at a specific index.

 

var todos = [
   { id: 1, completed: false, task: "Write a blog post" },
   { id: 2, completed: true, task: "Review pull requests" }
]
var idx = todos.findIndex(item => item.id === 1)
// toggle the completed of todo with the known idx
todos.with(idx, {...todos.at(idx), completed: !todos.at(idx).completed})

 

标签:idx,completed,ES2024,id,new,array,method,todos
From: https://www.cnblogs.com/Answer1215/p/18206160

相关文章

  • [Javascript] Perform Set Operations using JavaScript Set Methods
    The "SetMethods" proposalintroduces7newmethodstoJavaScript/ECMAScript:union(setB):ReturnsanewSetcontainingallelementsfromtheoriginalsetandanothersetsetB.Thismethodeffectivelycombinesthemembersoftwosetswithoutd......
  • 152- Maximum Produce Subarray-最大子数组之乘积
    问题描述Givenanintegerarray nums,finda subarray.thathasthelargestproduct,andreturn theproduct.Thetestcasesaregeneratedsothattheanswerwillfitina 32-bit integer.解释:找出一个数组中乘积最大的子数组,返回子数组的乘积。案例:Input......
  • Doug Lea大师的佳作CopyOnWriteArrayList,用不好能坑死你!
    一、写在开头我们在学习集合或者说容器的时候了解到,很多集合并非线程安全的,在并发场景下,为了保障数据的安全性,诞生了并发容器,广为人知的有ConcurrentHashMap、ConcurrentLinkedQueue、BlockingQueue等,那你们知道ArrayList也有自己对应的并发容器嘛?作为使用频率最高的集合类之一,A......
  • [Javascript] Find Items from the end of the JavaScript Array using at, findLast
    Findingelementsstartingfromtheendofanarrayhasgottenaloteasierwiththeintroductionofthe at, findLast,and findLastIndex methods!With at younolongerneedtoremembertoaccesstheendofthearraylike array[array.length-1] trick.......
  • LeetCode 918. Maximum Sum Circular Subarray
    原题链接在这里:https://leetcode.com/problems/maximum-sum-circular-subarray/description/题目:Givena circularintegerarray nums oflength n,return themaximumpossiblesumofanon-empty subarray of nums.A circulararray meanstheendofthearray......
  • JavaScript object array sort by string bug All In One
    JavaScriptobjectarraysortbystringbugAllInOnebug//purestringsarray,sortOK✅letarr=["banana","strawberry","apple"];JSON.stringify(arr.sort());//'["apple","banana","strawbe......
  • ArrayList和LinkedList区别
    底层数据结构ArrayList是动态数组的数据结构实现。LinkedList是双向链表的数据结构实现。效率下标査询ArrayList按照下标査询的时间复杂度O(1)。LinkedList不支持下标查询。查找(未知索引)ArrayList需要遍历,链表也需要链表,时间复杂度都是O(n)。新增和删除ArrayL......
  • 【java】【集合类】ArrayList扩容机制
    扩容规则1.ArrayList()无参构造扩容2.ArrayList(intinitialCapacity)扩容3.publicArrayList(Collection<?extendsE>c)扩容4.add(Objecto)扩容5.addAll(Collectionc)扩容要注意的是,以下所有代码中用反射方式来更直观地反映ArrayList的扩容特征,但从JDK9由于模......
  • D. Powerful array
    https://codeforces.com/contest/86/problem/D题意:n个数,m个查询。每个查询给出一个区间,查询的结果是区间内每个数出现次数的平方*该数的总和。思路:莫队算法。分块,查询排序,输出。总结:莫队算法适用于离线的情况,他的原理是将查询按左端点分块,块的大小是数量的开平方。然后对查询进......
  • CF1965D Missing Subarray Sum题解
    题目链接点击打开链接题目解法感觉一点都不好想\fn因为最后的序列\(a\)是回文的,所以只有以中心点对称的子段和出现次数为奇数,其他都为偶数考虑有没有什么知道所有子段和的做法,可以方便推出缺失一个的答案令\(g_{l,r}\)为\(l\)到\(r\)的子段和知道\(g_{1,n}\)删......