我有两个这样的数组:
var tendances= ["noyades", "noyade", "décès", "accidentelles", "auteurs"];
var nb_tendances = [10, 6, 4, 3, 3];
两个数组相互对应,如下所示:
noyades = 10
noyade = 6
décès = 4
accidentelles = 3
auteurs = 3
我想要什么:
tendances = ["noyade", "décès", "accidentelles", "auteurs"];
nb_tendances = [16, 4, 3, 3];
所以我的问题是:如何通过忽略最后一个's'来累积单词的复数形式和单数形式?我只想替换以“ s”结尾的单词,只要存在没有最后一个“ s”的单词
答案 0 :(得分:2)
使用Array.forEach
和Array.splice
var tendances= ["noyades", "noyade", "décès", "accidentelles", "auteurs"]
var nb_tendances = [10, 6, 4, 3, 3]
tendances.forEach(function(val, i) {
let index = tendances.indexOf(val + 's')
if(index === -1) return
nb_tendances[index] += nb_tendances[i]
tendances.splice(i, 1)
nb_tendances.splice(i, 1)
})
console.log(tendances)
console.log(nb_tendances)