Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
296 views
in Technique[技术] by (71.8m points)

javascript - How to filter objects with different keys in array?

I have objects like this one:

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

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...

Any ideas? Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...