JS实现替换list中的某个元素

for (let i = 0; i <  el.treeNodeList.length; i++) {
	if (el.treeNodeList[i].id===newChild[0].parentId){
	   let index =  el.treeNodeList[i].treeNodeList.findIndex(d => d.id === newChild[0].id);
	   el.treeNodeList[i].treeNodeList.splice(index,1,newChild[0]);
	 }
}
  1. if (el.treeNodeList[i].id===newChild[0].parentId){//找到treeNodeList中是否包含需要替换的元素
    
  2. findIndex找到需要替换的元素的下标
  3. splice替换掉旧元素
  4. 注:此思路仅供参考,记录一下

猜你喜欢

转载自blog.csdn.net/weixin_43866250/article/details/130921916
今日推荐