繁体   English   中英

如何将对象数组和嵌套 arrays 分解为带有单个数据的分隔 arrays

[英]How to disassemble an array of objects and nested arrays to Separated arrays with single data

我有一个这样的对象数组:

[
  {
    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:' 拆卸 object 并创建 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) ) );

在这里,我试图只获取昨天的警报,但它给了我所有在阵列中有日期的警报,它不会将阵列制动或拆分为单个 arrays。 我也尝试做嵌套的 forEach FN,不工作

我得到的最接近的是所有日期的数组,没有其他数据......

我希望有人可以帮助我:-)

你可以尝试类似的东西

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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM