function get20(arr){
let result = arr.filter((theArtist) => {
let birth = Number(theArtist.years.splice(0,3))
let death = Number(theArtist.years.splice(7,10))
return birth >= 1900 && death <= 2000;
})
return result;
}
这一直给我错误“theArtist.years.splice is not a function”我不明白为什么它不采用“years”字符串的前四个和最后四个字母并将它们转换为数字。年份字符串看起来像“1971 - 1984”
答案 0 :(得分:0)
Splice 是一种用于数组而不是字符串的方法。试试这个:
years = theArtist.years.split('')
let birth = Number(years.splice(0,4))
let death = Number(years.splice(7,4))
或者,使用子字符串。
let birth = Number(theArtist.years.substring(0,4))
let death = Number(theArtist.years.substring(7,11))
答案 1 :(得分:0)
您应该对字符串使用 substring
(splice
是数组的类似函数)。还要注意你的索引偏离了 1 !
var years = "1971 - 1984";
let birth = Number(years.substring(0,4))
let death = Number(years.substring(7,11))
console.log(birth,death)
答案 2 :(得分:0)
使用 slice
代替 splice
:
'1971 - 1984'.slice(0,4) // '1971'
'1971 - 1984'.slice(7,11) // '1984'