我有一个这样的对象数组:
[
{
alert_count: 3
alert_level: {value: 0, label: "ignore"}
count_dates: (3) ["2021-04-21T14:36:02.446Z", "2021-04-21T14:36:12.039Z", "2021-04-21T14:37:04.495Z"]
sound_type: {_id: "606331d3b0e1706ec7e55598", sound_type: "Finger snapping"}
sensor: {
sound_files: (3) ["60803852d78352ad8784220b", "6080385bd78352e29d84220d", 60803890d7835296b684221a"]
}
]我需要通过'count_dates:‘和'sound_files:’拆卸对象,并创建3个对象,每个对象都有一个日期和声音文件。如下所示:
[
{
alert_count: 1
alert_level: {value: 0, label: "ignore"}
count_dates: (1) ["2021-04-21T14:36:02.446Z"]
sound_type: {_id: "606331d3b0e1706ec7e55598", sound_type: "Finger snapping"}
sensor: {
sound_files: (1) ["60803852d78352ad8784220b"]
},
{
alert_count: 1
alert_level: {value: 0, label: "ignore"}
count_dates: (1) [ "2021-04-21T14:36:12.039Z"]
sound_type: {_id: "606331d3b0e1706ec7e55598", sound_type: "Finger snapping"}
sensor: {
sound_files: (1) [ "6080385bd78352e29d84220d"]
},
{
alert_count: 1
alert_level: {value: 0, label: "ignore"}
count_dates: (1) [ "2021-04-21T14:37:04.495Z"]
sound_type: {_id: "606331d3b0e1706ec7e55598", sound_type: "Finger snapping"}
sensor: {
sound_files: (1) [60803890d7835296b684221a"]
}
];我试着使用循环,过滤,map +过滤器。
示例:
const result = alert.filter(({ count_dates }) => count_dates.some( ({ alert }) => dateToString(alert) === dateToString(date - 1) ) );在这里,我试图只获取昨天的警报,但它给了我所有在数组中有日期的警报,它不会将数组停止或拆分为单个数组。我也尝试做嵌套的forEach FN,不起作用
我得到的最接近的是不含其他数据的所有日期的数组。
我希望有人能帮助我:-)
发布于 2021-04-22 19:41:02
你可以尝试像这样的东西
var a =[
{
alert_count: 3,
alert_level: {value: 0, label: "ignore"},
count_dates: ["2021-04-21T14:36:02.446Z", "2021-04-21T14:36:12.039Z", "2021-04-21T14:37:04.495Z"],
sound_type: {_id: "606331d3b0e1706ec7e55598", sound_type: "Finger snapping"},
sensor: {
sound_files: ["60803852d78352ad8784220b", "6080385bd78352e29d84220d", "60803890d7835296b684221a"]
}}];
console.log(a);
var mapped = a.map(function(item){
var ret = [];
for(var i=0;i<item.alert_count;i++){
ret.push({
alert_count: 1,
alert_level: item.alert_level,
count_dates: item.count_dates[i],
sound_type: item.sound_type,
sound_file: item.sensor.sound_files[i]
});
}
return ret;
})
console.log(mapped);https://stackoverflow.com/questions/67211526
复制相似问题