You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用JavaScript匹配数组对象id并更新price属性及传递旧值

问题需求

给定对象数组arrobj与参数paramid,需实现以下逻辑:

  • 找到arrobj中id值等于paramid的对象,将其price属性设为null,并记录该price的旧值oldValue;
  • 将oldValue按oldValue、oldValue+1、oldValue+2……的顺序,依次更新数组中剩余对象的price属性,仅更新那些原price大于oldValue的对象,且更新后的值需连续递增。

示例1

let paramid = 25
const arrobj = [
  { id: 10, price: null },
  { id: 12, price: 1 },
  { id: 14, price: 2 },
  { id: 25, price: 3 }, 
  { id: 23, price: 4 },
  { id: 43, price: 5 },
];

匹配id=25的对象,price设为null,旧值3依次更新id=23的price为3,id=43的price为4

预期输出:

[
  { id: 10, price: null },
  { id: 12, price: 1 },
  { id: 14, price: 2 },
  { id: 25, price: null }, 
  { id: 23, price: 3 },
  { id: 43, price: 4 },
];

示例2

let paramid = 43
const arrobj = [
  { id: 10, price: null },
  { id: 13, price: 1 },
  { id: 14, price: 2 },
  { id: 25, price: 5 }, 
  { id: 23, price: 6 },
  { id: 43, price: 4 },
  { id: 28, price: 7 }
];

匹配id=43的对象,price设为null,旧值4依次更新id=25的price为4,id=23的price为5,id=28的price为6

预期输出:

[
  { id: 10, price: null },
  { id: 13, price: 1 },
  { id: 14, price: 2 },
  { id: 25, price: 4 }, 
  { id: 23, price: 5 },
  { id: 43, price: null },
  { id: 28, price: 6 }
];

已尝试代码

const getSameId =  arrobj(e=>e.id === paramid);

let oldValue = getSameId.price;

for (let item of arrobj) {
  const index = arrobj.index(item)
  if(item.id === paramid) {
   ...item,
   price: null 
  }
  if(item.id !== paramid && oldValue > item.price) {
   ...item,
   price: oldValue + 1 
  }
}

修正后的实现代码

function updatePrices(arrobj, paramid) {
  // 找到目标对象并记录旧值
  const targetObj = arrobj.find(item => item.id === paramid);
  if (!targetObj) return [...arrobj]; // 未找到目标id时返回原数组副本

  const oldValue = targetObj.price;
  // 创建数组副本,避免修改原数组
  const newArr = [...arrobj];
  // 将目标对象的price设为null
  const targetIndex = newArr.findIndex(item => item.id === paramid);
  newArr[targetIndex] = {...newArr[targetIndex], price: null};

  // 筛选需要更新的对象:price不为null且原price大于oldValue
  const needUpdate = newArr.filter(item => item.price !== null && item.price > oldValue);
  // 按原price从小到大排序,保证更新顺序正确
  needUpdate.sort((a, b) => a.price - b.price);

  // 依次赋值为连续递增的数值
  needUpdate.forEach((item, idx) => {
    item.price = oldValue + idx;
  });

  return newArr;
}

代码说明

  • 用find定位目标对象,记录oldValue,同时创建数组副本避免污染原数据;
  • 筛选出符合条件的更新对象:排除price为null的元素,只保留原price大于oldValue的;
  • 对需更新的元素按原price升序排序,确保赋值顺序匹配示例逻辑;
  • 遍历排序后的数组,通过oldValue + 索引实现连续递增的赋值;
  • 最终返回处理后的数组副本。

测试验证

示例1测试:

let paramid = 25
const arrobj = [
  { id: 10, price: null },
  { id: 12, price: 1 },
  { id: 14, price: 2 },
  { id: 25, price: 3 }, 
  { id: 23, price: 4 },
  { id: 43, price: 5 },
];
console.log(updatePrices(arrobj, paramid));
// 输出与示例1预期一致

示例2测试:

let paramid = 43
const arrobj = [
  { id: 10, price: null },
  { id: 13, price: 1 },
  { id: 14, price: 2 },
  { id: 25, price: 5 }, 
  { id: 23, price: 6 },
  { id: 43, price: 4 },
  { id: 28, price: 7 }
];
console.log(updatePrices(arrobj, paramid));
// 输出与示例2预期一致

内容的提问来源于stack exchange,提问作者code24

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 04:17:20