如何过滤数组中具有不同键的对象? [英] How to filter objects with different keys in array?

查看:132
本文介绍了如何过滤数组中具有不同键的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的对象:

{
      "name": "Ola",
      "dates": [
        {
          "7.01.2020": [1, 2, 3]
        },
        {
          "8.01.2020": [4, 5, 6]
        },
        {
          "9.01.2020": [7, 8, 9]
        }
      ],
      "id": 7
    }

,我需要过滤日期对象以检查是否存在是特定日期并返回其值(例如,如果存在7.01.2020)。当我从前user.dates尝试时,我得到了包含3个不同对象的数组,但是当我使用filteror map方法时,它不起作用。

and I need to filter through dates object to check if there is specific date and return it's values (ex. if there is 7.01.2020). When I try fro ex user.dates, I get array with 3 different objects but when I use filteror map method, it doesn't work.

当用户选择日期时,我需要检查该日期是否存在并将任务添加到现有日期中,如果是新日期,则添加带有任务的日期...

When user pick date I need to check if this date exists and add task to the existing ones and if it's new date, add date with tasks...

有什么想法吗?谢谢!

推荐答案

实际上,如果它不适用于过滤器和地图,则实际上需要使用 for循环。我建议您根据上述数组中的键访问日期

Actually you need to use for loop if it's not working with filter and map. I would suggest you to access date according to keys from the above arrays

let x = {
      "name": "Ola",
      "dates": [
        {
          "7.01.2020": [1, 2, 3]
        },
        {
          "8.01.2020": [4, 5, 6]
        },
        {
          "9.01.2020": [7, 8, 9]
        }
      ],
      "id": 7
    }
    
    let y = Object.values(x.dates);
    for(const [key,value] of Object.entries(y))
    {
    //getting particular date string
      result = Object.keys(value);
      console.log(result);
      //converted date for result
      date = new Date(result);
      console.log(date);
    }


请更改,包括您要求的返回日期逻辑。

please change include your required logic for return date.

这篇关于如何过滤数组中具有不同键的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆