[英]Remove array in array using splice issues
I have and array added_collections and current state is:我有数组 added_collections 和当前 state 是:
added_collections
(2) [Array(2), Array(1)]
0: Array(2)
0: []
1: [{…}]
length: 2
[[Prototype]]: Array(0)
1: [Array(10)]
length: 2
I need to remove an product from collection so I write:我需要从收藏中删除一个产品,所以我写:
added_collections[0][1].splice(0, 1);
so the result I expect to get is:所以我期望得到的结果是:
added_collections
(2) [Array(2), Array(1)]
0: Array(2)
0: []
1: []
length: 2
[[Prototype]]: Array(0)
1: [Array(10)]
length: 2
[[Prototype]]: Array(0)
but instead that I get:但相反,我得到:
(2) [Array(2), Array(1)]
0: Array(0)
length: 0
[[Prototype]]: Array(0)
1: [Array(10)]
length: 2
Why its two empty arrays are merged?为什么它的两个空 arrays 被合并了?
When I follow steps in browser console everything is fine but when I run the same code to execute its not the same results and seems that 2 empty arrays missing当我按照浏览器控制台中的步骤进行操作时,一切都很好,但是当我运行相同的代码来执行其不同的结果时,似乎缺少 2 个空 arrays
UPDATE: this is the full code of added_collection[0]:更新:这是 added_collection[0] 的完整代码:
'[[[],[{"id":32620843270280,"product_id":4632299831432,"title":"3 / red","price":"249.00","sku":"DM-03-red-3","position":1,"inventory_policy":"deny","compare_at_price":"0.00","fulfillment_service":"manual"}]]]'
and I run js code:我运行js代码:
let coll_no = 0;
let page_no = 1;
let product_no = 0;
added_collections[coll_no][page_no].splice(product_no, 1);
Works fine for me对我来说很好
const collection = [ [ [], [{ "id": 32620843270280, "product_id": 4632299831432, "title": "3 / red", "price": "249.00", "sku": "DM-03-red-3", "position": 1, "inventory_policy": "deny", "compare_at_price": "0.00", "fulfillment_service": "manual" }] ] ] collection[0][1].splice(0, 1) console.log(collection)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.