js Array.some(item =>{}) 中item类型与item的关系

当Array中存储的元素 item 为基本数据类型时,对 item 的修改不会影响原数组

let s = [1,2,3]
s.some(item => {item = 1})
s // [1,2,3]

当 item 为对象亦或是数组时 ,item直接指向其地址,

let s = [
  {a:123},
  {a:456},
  {a:789}
]
s.some(item => {item.a = 1})
s // [{a:1},{a:1},{a:1}]

原数组被修改

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以优化这段代码,使其更加简洁易读。可以使用一些函数,例如 Array.prototype.find() 和 Array.prototype.findIndex() 来代替嵌套的循环语句。另外,可以使用一些变量来存储一些重复的值,以便于以后的使用。下面是一个可能的优化版本: ```javascript const shopIndex = currentProduct.findIndex(item => item.shopId === shopId); if (shopIndex === -1) { currentProduct.push({ shopId, tenant: [{ tenantId, orderItem: [productInfo], }], }); } else { const tenantIndex = currentProduct[shopIndex].tenant.findIndex(val => val.tenantId === tenantId); if (tenantIndex === -1) { currentProduct[shopIndex].tenant.push({ tenantId, orderItem: [productInfo], }); } else { const productIndex = currentProduct[shopIndex].tenant[tenantIndex].orderItem.findIndex(pro => pro.tenantMaterialId === tenantMaterialId); if (productIndex === -1) { currentProduct[shopIndex].tenant[tenantIndex].orderItem.push(productInfo); } else { currentProduct[shopIndex].tenant[tenantIndex].orderItem[productIndex].num = num; } } } ``` 这段代码首先查找当前店铺是否已经存在于 `currentProduct` 数组。如果不存在,则新建一个对象,并将该对象添加到数组。如果存在,则查找当前商户是否已经存在于该店铺。如果不存在,则新建一个对象,并将该对象添加到商户数组。如果存在,则查找当前商品是否已经存在于该商户的订单列表。如果不存在,则将该商品添加到订单列表。如果存在,则更新该商品的数量。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值