我无法理解我的代码有什么问题。
这块逻辑应该没问题
const r = x.map(obj => {
if (obj.hour <= 10 || obj.hour >= 22) {
return obj
}
})
console.log(r)
但错误在某处。 https://jsfiddle.net/her7uzps/
我试图抓住json,也没关系。我试图创建一个index.html而不是使用jsfiddle,它仍然无法显示哪一行有问题。
答案 0 :(得分:3)
你的错误不存在。您的错误位于代码的开头,您编写了cosnt
并且应该放置const
。
答案 1 :(得分:0)
在声明数组时,您拼错了const
这个词。而Array#filter
取而代之的是map
,很容易用条件重建数组
const x = [{
"hour": 0,
"total_count": 0,
"male_count": 0,
"female_count": 0
}, {
"hour": 1,
"total_count": 0,
"male_count": 0,
"female_count": 0
}, {
"hour": 2,
"total_count": 0,
"male_count": 0,
"female_count": 0
}, {
"hour": 3,
"total_count": 0,
"male_count": 0,
"female_count": 0
}, {
"hour": 4,
"total_count": 0,
"male_count": 0,
"female_count": 0
}, {
"hour": 5,
"total_count": 0,
"male_count": 0,
"female_count": 0
}, {
"hour": 6,
"total_count": 0,
"male_count": 0,
"female_count": 0
}, {
"hour": 7,
"total_count": 1,
"male_count": 0,
"female_count": 1
}, {
"hour": 8,
"total_count": 21,
"male_count": 3,
"female_count": 18
}, {
"hour": 9,
"total_count": 230,
"male_count": 110,
"female_count": 120
}, {
"hour": 10,
"total_count": 308,
"male_count": 151,
"female_count": 157
}, {
"hour": 11,
"total_count": 238,
"male_count": 103,
"female_count": 135
}, {
"hour": 12,
"total_count": 236,
"male_count": 128,
"female_count": 108
}, {
"hour": 13,
"total_count": 237,
"male_count": 153,
"female_count": 84
}, {
"hour": 14,
"total_count": 234,
"male_count": 130,
"female_count": 104
}, {
"hour": 15,
"total_count": 180,
"male_count": 99,
"female_count": 81
}, {
"hour": 16,
"total_count": 203,
"male_count": 117,
"female_count": 86
}, {
"hour": 17,
"total_count": 235,
"male_count": 139,
"female_count": 96
}, {
"hour": 18,
"total_count": 194,
"male_count": 111,
"female_count": 83
}, {
"hour": 19,
"total_count": 149,
"male_count": 89,
"female_count": 60
}, {
"hour": 20,
"total_count": 38,
"male_count": 20,
"female_count": 18
}, {
"hour": 21,
"total_count": 1,
"male_count": 1,
"female_count": 0
}, {
"hour": 22,
"total_count": 1,
"male_count": 1,
"female_count": 0
}, {
"hour": 23,
"total_count": 0,
"male_count": 0,
"female_count": 0
}]
const r = x.filter(obj => (obj.hour <= 10 || obj.hour >= 22) )
console.log(r)