我正在尝试通过过滤现有对象来创建对象。
我要实现的目标是过滤内部对象activities
并仅获取completed_at
以下的所有1546964467
值。
我现在尝试的是:
this.weekHabits = this.formattedHabits.filter(habit =>
habit.activities.map((item) =>({
activities: item.activities.completed_at.filter(
value => value = 1546646400
)
}))
)
但是显然是错误的
感谢您的耐心等待,我们将不胜感激
这是原始对象:
[
{
"frequency":[
7,
6,
2,
1
],
"description":"Walk 100km",
"color":"#E97825",
"activities":[
{
"completed":true,
"completed_at":1546646400
},
{
"completed":true,
"completed_at":1546819200
},
{
"completed":true,
"completed_at":1546905600
},
{
"completed":true,
"completed_at":1547251200
},
{
"completed":false,
"completed_at":1547424000
},
{
"completed":true,
"completed_at":1547510400
},
{
"completed":true,
"completed_at":1547856000
},
{
"completed":false,
"completed_at":1548028800
}
]
},
{
"frequency":[
5,
4,
3,
1
],
"description":"Drink 4lt Water",
"color":"#ED1E24",
"activities":[
{
"completed":true,
"completed_at":1546387200
},
{
"completed":true,
"completed_at":1546473600
},
{
"completed":false,
"completed_at":1546560000
},
{
"completed":false,
"completed_at":1546819200
},
{
"completed":true,
"completed_at":1546992000
},
{
"completed":false,
"completed_at":1547078400
},
{
"completed":false,
"completed_at":1547164800
},
{
"completed":false,
"completed_at":1547424000
},
{
"completed":true,
"completed_at":1547596800
},
{
"completed":false,
"completed_at":1547683200
},
{
"completed":true,
"completed_at":1547769600
},
{
"completed":true,
"completed_at":1548028800
}
]
},
{
"frequency":[
7,
6,
5,
4,
3,
2,
1
],
"description":"Study 2",
"color":"#F4ED59",
"activities":[
{
"completed":false,
"completed_at":1546387200
},
{
"completed":true,
"completed_at":1546473600
},
{
"completed":true,
"completed_at":1546560000
},
{
"completed":true,
"completed_at":1546646400
},
{
"completed":true,
"completed_at":1546819200
},
{
"completed":false,
"completed_at":1546905600
},
{
"completed":true,
"completed_at":1546992000
},
{
"completed":true,
"completed_at":1547078400
},
{
"completed":false,
"completed_at":1547164800
},
{
"completed":true,
"completed_at":1547251200
},
{
"completed":true,
"completed_at":1547424000
},
{
"completed":false,
"completed_at":1547510400
},
{
"completed":true,
"completed_at":1547596800
},
{
"completed":false,
"completed_at":1547683200
},
{
"completed":true,
"completed_at":1547769600
},
{
"completed":false,
"completed_at":1547856000
},
{
"completed":false,
"completed_at":1548028800
}
]
},
{
"frequency":[
7,
6,
5,
4
],
"description":"Home drinking food2",
"color":"#00A651",
"activities":[
{
"completed":true,
"completed_at":1546473600
},
{
"completed":true,
"completed_at":1546560000
},
{
"completed":true,
"completed_at":1546646400
},
{
"completed":true,
"completed_at":1547078400
},
{
"completed":true,
"completed_at":1547164800
},
{
"completed":true,
"completed_at":1547251200
},
{
"completed":false,
"completed_at":1547683200
},
{
"completed":true,
"completed_at":1547769600
},
{
"completed":true,
"completed_at":1547856000
}
]
},
{
"frequency":[
7,
4,
3,
2,
1
],
"description":"New habit 4",
"color":"#912AD6",
"activities":[
{
"completed":true,
"completed_at":1546387200
},
{
"completed":true,
"completed_at":1546473600
},
{
"completed":true,
"completed_at":1546819200
},
{
"completed":true,
"completed_at":1546905600
},
{
"completed":false,
"completed_at":1546992000
},
{
"completed":true,
"completed_at":1547078400
},
{
"completed":false,
"completed_at":1547424000
},
{
"completed":true,
"completed_at":1547510400
},
{
"completed":true,
"completed_at":1547596800
},
{
"completed":true,
"completed_at":1547683200
},
{
"completed":true,
"completed_at":1548028800
}
]
}
]
答案 0 :(得分:0)
您在代码中遗漏了一些东西。
比较和赋值不是同一回事。此value = 1546646400
应该在过滤器中的value <= 1546646400
let obj = [{"frequency":[7,6,2,1],"description":"Walk 100km","color":"#E97825","activities":[{"completed":true,"completed_at":1546646400},{"completed":true,"completed_at":1546819200},{"completed":true,"completed_at":1546905600},{ "completed":true,"completed_at":1547251200},{ "completed":false,"completed_at":1547424000 },{ "completed":true,"completed_at":1547510400},{ "completed":true,"completed_at":1547856000},{ "completed":false,"completed_at":1548028800}]},{"frequency":[ 5,4,3,1],"description":"Drink 4lt Water","color":"#ED1E24", "activities":[{"completed":true,"completed_at":1546387200},"completed":true,"completed_at":1546473600},"completed":false,"completed_at":1546560000},{"completed":false, "completed_at":1546819200},{"completed":true,"completed_at":1546992000},{"completed":false,"completed_at":1547078400 },{"completed":false, "completed_at":1547164800 },{"completed":false,"completed_at":1547424000}, {"completed":true,"completed_at":1547596800},"completed":false,"completed_at":1547683200},{"completed":true,"completed_at":1547769600},{"completed":true,"completed_at":1548028800}]},{"frequency":[7,6,5,4,3,2,1],"description":"Study 2","color":"#F4ED59","activities":[{"completed":false,"completed_at":1546387200},{"completed":true,"completed_at":1546473600},{"completed":true,"completed_at":1546560000},{"completed":true,"completed_at":1546646400},{"completed":true,"completed_at":1546819200},{"completed":false,"completed_at":1546905600},{"completed":true,"completed_at":1546992000},{"completed":true,"completed_at":1547078400},{"completed":false,"completed_at":1547164800},{"completed":true,"completed_at":1547251200},{"completed":true,"completed_at":1547424000},{"completed":false,"completed_at":1547510400},{"completed":true,"completed_at":1547596800},{"completed":false,"completed_at":1547683200},{"completed":true,"completed_at":1547769600},{"completed":false,"completed_at":1547856000},{"completed":false,"completed_at":1548028800}]},{"frequency":[7,6,5,4],"description":"Home drinking food2","color":"#00A651","activities":[{"completed":true,"completed_at":1546473600},{"completed":true,"completed_at":1546560000},{"completed":true,"completed_at":1546646400},{"completed":true,"completed_at":1547078400},{"completed":true,"completed_at":1547164800},{"completed":true,"completed_at":1547251200},{"completed":false,"completed_at":1547683200},{"completed":true,"completed_at":1547769600},{"completed":true,"completed_at":1547856000}]},{"frequency":[7,4,3,2,1],"description":"New habit 4","color":"#912AD6","activities":[{"completed":true,"completed_at":1546387200},{"completed":true,"completed_at":1546473600},
{
"completed":true,
"completed_at":1546819200},{"completed":true,"completed_at":1546905600},{"completed":false,"completed_at":1546992000},{"completed":true,"completed_at":1547078400},{"completed":false,"completed_at":1547424000},{"completed":true,"completed_at":1547510400},{"completed":true,"completed_at":1547596800},{"completed":true,"completed_at":1547683200},{"completed":true,"completed_at":1548028800}]}]
let op = obj.map(ele=>{
let {activities} = ele
let selected = activities.filter(element=> element.completed_at <= 1546646400 )
return {
...ele,
activities: selected
}
})
console.log(op)
答案 1 :(得分:0)
我认为(看一下您实现的代码)您想要这样的东西,即保留所有对象结构,但我们过滤activities
数组以仅保留那些满足条件{{1 }}:
completed_at <= <some_number>
答案 2 :(得分:0)
您在那儿混合了一些东西,您尝试使用map
的响应作为过滤条件,然后您使用的地图应该是过滤器,基本上就是您所做的工作的一部分。 / p>
我认为这就是您想要的:
this.formattedHabits = [{
"frequency": [
7,
6,
2,
1
],
"description": "Walk 100km",
"color": "#E97825",
"activities": [{
"completed": true,
"completed_at": 1546646400
},
{
"completed": true,
"completed_at": 1546819200
},
{
"completed": true,
"completed_at": 1546905600
},
{
"completed": true,
"completed_at": 1547251200
},
{
"completed": false,
"completed_at": 1547424000
},
{
"completed": true,
"completed_at": 1547510400
},
{
"completed": true,
"completed_at": 1547856000
},
{
"completed": false,
"completed_at": 1548028800
}
]
},
{
"frequency": [
5,
4,
3,
1
],
"description": "Drink 4lt Water",
"color": "#ED1E24",
"activities": [{
"completed": true,
"completed_at": 1546387200
},
{
"completed": true,
"completed_at": 1546473600
},
{
"completed": false,
"completed_at": 1546560000
},
{
"completed": false,
"completed_at": 1546819200
},
{
"completed": true,
"completed_at": 1546992000
},
{
"completed": false,
"completed_at": 1547078400
},
{
"completed": false,
"completed_at": 1547164800
},
{
"completed": false,
"completed_at": 1547424000
},
{
"completed": true,
"completed_at": 1547596800
},
{
"completed": false,
"completed_at": 1547683200
},
{
"completed": true,
"completed_at": 1547769600
},
{
"completed": true,
"completed_at": 1548028800
}
]
},
{
"frequency": [
7,
6,
5,
4,
3,
2,
1
],
"description": "Study 2",
"color": "#F4ED59",
"activities": [{
"completed": false,
"completed_at": 1546387200
},
{
"completed": true,
"completed_at": 1546473600
},
{
"completed": true,
"completed_at": 1546560000
},
{
"completed": true,
"completed_at": 1546646400
},
{
"completed": true,
"completed_at": 1546819200
},
{
"completed": false,
"completed_at": 1546905600
},
{
"completed": true,
"completed_at": 1546992000
},
{
"completed": true,
"completed_at": 1547078400
},
{
"completed": false,
"completed_at": 1547164800
},
{
"completed": true,
"completed_at": 1547251200
},
{
"completed": true,
"completed_at": 1547424000
},
{
"completed": false,
"completed_at": 1547510400
},
{
"completed": true,
"completed_at": 1547596800
},
{
"completed": false,
"completed_at": 1547683200
},
{
"completed": true,
"completed_at": 1547769600
},
{
"completed": false,
"completed_at": 1547856000
},
{
"completed": false,
"completed_at": 1548028800
}
]
},
{
"frequency": [
7,
6,
5,
4
],
"description": "Home drinking food2",
"color": "#00A651",
"activities": [{
"completed": true,
"completed_at": 1546473600
},
{
"completed": true,
"completed_at": 1546560000
},
{
"completed": true,
"completed_at": 1546646400
},
{
"completed": true,
"completed_at": 1547078400
},
{
"completed": true,
"completed_at": 1547164800
},
{
"completed": true,
"completed_at": 1547251200
},
{
"completed": false,
"completed_at": 1547683200
},
{
"completed": true,
"completed_at": 1547769600
},
{
"completed": true,
"completed_at": 1547856000
}
]
},
{
"frequency": [
7,
4,
3,
2,
1
],
"description": "New habit 4",
"color": "#912AD6",
"activities": [{
"completed": true,
"completed_at": 1546387200
},
{
"completed": true,
"completed_at": 1546473600
},
{
"completed": true,
"completed_at": 1546819200
},
{
"completed": true,
"completed_at": 1546905600
},
{
"completed": false,
"completed_at": 1546992000
},
{
"completed": true,
"completed_at": 1547078400
},
{
"completed": false,
"completed_at": 1547424000
},
{
"completed": true,
"completed_at": 1547510400
},
{
"completed": true,
"completed_at": 1547596800
},
{
"completed": true,
"completed_at": 1547683200
},
{
"completed": true,
"completed_at": 1548028800
}
]
}
]
this.weekHabits = this.formattedHabits.map(habit =>
habit.activities.filter(item => item.completed_at <= 1546964467)
)
console.log(this.weekHabits)