Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/452.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在插入元素之前,如何重命名元素?_Javascript - Fatal编程技术网

Javascript 在插入元素之前,如何重命名元素?

Javascript 在插入元素之前,如何重命名元素?,javascript,Javascript,我想将我的新列表(newList)的元素添加到我已经存在的列表(oldList)中,但在插入之前,我必须验证我的新列表的元素是否已经存在于我的oldList中,如果已经存在,则必须添加额外的编号 let newList=[{name:“abc.jpeg”},{name:“abc.jpeg”}] 让oldList=[{name:“fs%2FPanel%8SmgM7rhJ4T9j%2Fpictures%2Fa.jpeg”}, {name:“fs%2FPanel%8SmgM7rhJ4T9j%2Fpi

我想将我的新列表(newList)的元素添加到我已经存在的列表(oldList)中,但在插入之前,我必须验证我的新列表的元素是否已经存在于我的oldList中,如果已经存在,则必须添加额外的编号

let newList=[{name:“abc.jpeg”},{name:“abc.jpeg”}]
让oldList=[{name:“fs%2FPanel%8SmgM7rhJ4T9j%2Fpictures%2Fa.jpeg”},
{name:“fs%2FPanel%8SmgM7rhJ4T9j%2Fpictures%2Fa1.jpeg”},
{name:“fs%2FPanel%8SmgM7rhJ4T9j%2Fpictures%2Fa2.jpeg”}]
常量添加元素=(集合、新项目、计数)=>{
const a=新的_item.name.split('.'))
const reps=collection.filter(元素=>{
var reg=/.*2F图片%2F(.*)/
常量名称=元素名称匹配(reg)
如果(姓名)
返回名称[1]。包括(a[0])
}).长度
new_item.sname=a[0]+(重复次数>0?重复次数:“”)+'.+a[1]
collection.push(新项目)
console.log(collection)
回采
}
让tally={}
for(让index=0;index}
您可以首先找到字符串的出现处,然后将该数字附加到字符串中

let newList=[{name:“abc1.jpeg”},{name:“abc1.jpeg”}]
让oldList=[{name:“fs%2FPanel%8SmgM7rhJ4T9j%2Fpictures%2Fa.jpeg”},
{name:“fs%2FPanel%8SmgM7rhJ4T9j%2Fpictures%2Fa1.jpeg”},
{name:“fs%2FPanel%8SmgM7rhJ4T9j%2Fpictures%2Fa2.jpeg”}]
常量添加元素=(集合、新项目、计数)=>{
const a=新的_item.name.split('.'))
const occurrence=collection.filter(item=>item.name==new\u item.name).length;
如果(发生率>0){
const firstPart=[…a.slice(0,-1)].join(“.”);
const digitMatch=firstPart.match(/(\d+)$/);
常量digitString=digitMatch&&digitMatch[0];
const firstpartwithout digit=digitString&&firstPart.slice(0,-digitString.length)
const digit=digitString&&parseInt(digitString);
常量newDigit=(数字| | 0)+出现次数;
常量前缀=没有数字的第一部分| |第一部分;
new_item.name=前缀+newDigit+“+a[a.length-1]
//[…a.slice(0,-1)]。连接(“.”->最后一个零件之前的零件。
//发生->文件名在集合中存在的次数
//a[a.length-1]->之后字符串的最后一部分。
}
const reps=collection.filter(元素=>{
var reg=/.*2F图片%2F(.*)/
常量名称=元素名称匹配(reg)
如果(姓名)
返回名称[1]。包括(a[0])
}).长度
new_item.sname=a[0]+(重复次数>0?重复次数:“”)+'.+a[1]
collection.push(新项目)
console.log(collection)
回采
}
让tally={}
for(让index=0;index}
另一种方法是使用从
计数开始的方法,这是一种映射,比使用多个
filter()实例进行查找要高效得多。

let newList=[{name:“abc.jpeg”},{name:“abc.jpeg”},{name:“fs%2FPanel%8SmgM7rhJ4T9j%2Fpictures%2Fa.jpeg”}]
让oldList=[{name:“fs%2FPanel%8SmgM7rhJ4T9j%2Fpictures%2Fa.jpeg”},
{name:“fs%2FPanel%8SmgM7rhJ4T9j%2Fpictures%2Fa1.jpeg”},
{name:“fs%2FPanel%8SmgM7rhJ4T9j%2Fpictures%2Fa2.jpeg”},{name:“abc.jpeg”}];
常量baseName=(str)=>str.replace(/\d+\./,'.');
const getNamesCount=arr=>arr.reduce((a,{name})=>{
const base=baseName(name);
返回a.set(base,(a.get(base)| | 0)+1)
},新映射())
const mergelist=(oList,nList)=>{
const-tally=getNamesCount(oList);
nList.forEach(({name,…rest})=>{
const base=baseName(name);
如果(计数有(基数)){
让计数=计数。获取(基本)
name=name.split('.').join(count+'.');
计数设置(基数+计数)
}否则{
理货台(底座,1)
}
oList.push({name,…rest})
})
回归寡头;
}
log(合并列表(旧列表、新列表))
//我添加了一些用于测试的元素
让newList=[
{name:“abc.jpeg”},
{name:“abc.jpeg”},
{name:“xxx123.jpg”},
{name:“xxx123.jpg”},
{name:“xxx123.jpg”},
{name:“xxx123.jpg”},
{name:“yyy143.jpg”},
{name:“yyy143.jpg”},
{name:“yyy143.jpg”},
{name:“yyyy144.jpg”},
{name:“yyy145.jpg”},
{name:“yyyy146.jpg”}
];
让oldList=[
{name:“fs%2FApanel%8SmgM7rhJ4T9j%2FAPictures%2Fa.jpeg”},
{name:“fs%2FPanel%8SmgM7rhJ4T9j%2Fpictures%2Fa1.jpeg”},
{name:“fs%2FPanel%8SmgM7rhJ4T9j%2Fpictures%2Fa2.jpeg”}];
//创建集合
const mySet=新集合()
//用文件名填充mySet
对于(i=0;i=0){
//将数字和1相加
num=parseInt(numbers.join(“”)+1;
snum=字符串(num);
//不带数字的名称部分
name=name.substring(0,name.length-number.length);
//当集合中有文件名时,再次求和1
而(mySet.has(name+snum+ext)){
snum=字符串(num++);
}
//在这里,我们有一个新的文件名在集合中不存在
//因此,将其添加到具有编号和扩展名的集合中
添加(name+snum+ext);
}否则{
//文件名重复,但是
//没有其他人有数字
//因此,在名称和扩展名中添加“1”字符串
//并将其添加到集合中
名称+=“1”;
mySet.add(name+ext);
}
}否则{
//文件名不在oldlist中,因此我们可以将其添加到集合中
添加(newList[i].name);
}
}
//重置旧列表数组
oldList=[];
//像以前一样使用对象填充它,但使用新的集合项作为值
对于(mySet的let项){
oldList.push({name:item})
}
//表现出来

console.log(oldList)在这种情况下会发生什么让newList=[{name:“abc.jpeg”},{name:“abc.jpeg”}让oldList=[{name:“fs%2FPanel%8SmgM7rhJ4T9j%2Fpictures%2Fa.jpeg”},{name:“fs%2FPanel%8SmgM7rhJ4T9j%2Fpictures%2Fa1.jpeg”},{name:“fs%2FPanel%8SmgM7rhJ4T9j%2Fpictures”