搜索js对象找到field属性值是daoqiriqi进行整个单一{}对象替换

      发布在:前端技术      评论:0 条评论

可以使用以下代码实现:

// 定义原始对象
const obj = [{"field":"xingming","title":"姓名","search":true,"width":"68"},{"field":"zhuanyemingcheng","title":"专业名称","search":true,"width":"150"},{"field":"shenfenzhenghao","title":"身份证号","search":true,"width":"177"},{"field":"zhengshubianhao","title":"证书编号","search":true,"width":"140"},{"field":"zhengshumingcheng","title":"证书名称","search":true,"width":"170"},{"field":"birthdate","title":"出生年月","search":false,"width":"120","templet":"ea.table.date"},{"field":"daoqiriqi","title":"到期日期","search":false,"width":"110","templet":"ea.table.date"}],"statusList":{"-1":"审核不通过","0":"待审核","1":"审核通过"}};

// 遍历数组,查找field属性值为"daoqiriqi"的对象
for (let i = 0; i < obj.length; i++) {
  if (obj[i].field === "daoqiriqi") {
    // 替换整个对象
    obj[i] = { "field": "newField", "title": "新标题", "search": true, "width": "100" };
    break; // 找到后跳出循环
  }
}

console.log(obj); // 输出替换后的对象

在上述代码中,我们首先定义了一个原始对象`obj`,然后使用`for`循环遍历数组,查找`field`属性值为`"daoqiriqi"`的对象。当找到该对象时,我们使用一个新的对象替换整个对象。最后输出替换后的对象。

相关文章
热门推荐