0

I am stuck a little on how to correctly make an array based on 2 other arrays.

I have one array like this:

let date = [1522303200000, 1522389600000, 1522476000000]

and the second array looks somewhat like this:

let people = [
  {
    "id": 0,
    "name": "Sophia Mason",
    "biography": "Lorem ...",
    "availability": [
      {
        "date": 1522303200000,
        "times": ["9:00 am", "2:30 pm", "5:00 pm", "6:00 pm"]
      },
      {
        "date": 1522389600000,
        "times": ["9:00 am", "2:30 pm", "5:00 pm", "6:00 pm"]
      },
      {
        "date": 1522476000000,
        "times": ["10:00 am", "2:30 pm", "5:00 pm"]
      },
      {
        "date": 1522735200000,
        "times": ["9:00 am", "2:30 pm", "5:00 pm", "6:00 pm"]
      }
    ]
  },
  {
    "id": 1,
    "name": "Brandon Hampton",
    "biography": "Lorem ...",
    "availability": [
      {
        "date": 1522303200000,
        "times": ["9:00 am", "2:30 pm", "5:00 pm", "6:00 pm"]
      },
      {
        "date": 1522476000000,
        "times": ["10:00 am", "2:30 pm", "5:00 pm"]
      },
      {
        "date": 1522648800000,
        "times": ["9:00 am", "2:30 pm", "5:00 pm", "6:00 pm"]
      },
      {
        "date": 1522735200000,
        "times": ["9:00 am", "2:30 pm", "5:00 pm", "6:00 pm"]
      }
    ]
  }
]

I am trying to create an array that has the date as the top level of each index in the array and then each corresponding person in each day if there is availability for that date. I am thinking something similar to this:

let calendar = [
    [
      1522303200000,
      "photographers": [
        {
          "id": 0,
          "name": "Sophia Mason",
          "biography": "Lorem ...",
          "availability": [
            {
              "date": 1522303200000,
              "times": ["9:00 am", "2:30 pm", "5:00 pm", "6:00 pm"]
            },
            {
              "date": 1522389600000,
              "times": ["9:00 am", "2:30 pm", "5:00 pm", "6:00 pm"]
            },
            {
              "date": 1522476000000,
              "times": ["10:00 am", "2:30 pm", "5:00 pm"]
            },
            {
              "date": 1522735200000,
              "times": ["9:00 am", "2:30 pm", "5:00 pm", "6:00 pm"]
            }
          ]
        },
        {
          "id": 1,
          "name": "Brandon Hampton",
          "biography": "Lorem ...",
          "availability": [
            {
              "date": 1522303200000,
              "times": ["9:00 am", "2:30 pm", "5:00 pm", "6:00 pm"]
            },
            {
              "date": 1522476000000,
              "times": ["10:00 am", "2:30 pm", "5:00 pm"]
            },
            {
              "date": 1522648800000,
              "times": ["9:00 am", "2:30 pm", "5:00 pm", "6:00 pm"]
            },
            {
              "date": 1522735200000,
              "times": ["9:00 am", "2:30 pm", "5:00 pm", "6:00 pm"]
            }
          ]
        }
      ]
    ],
    [
      1522303200000,
      "photographers": [
        {
          "id": 0,
          "name": "Sophia Mason",
          "biography": "Lorem ...",
          "availability": [
            {
              "date": 1522303200000,
              "times": ["9:00 am", "2:30 pm", "5:00 pm", "6:00 pm"]
            },
            {
              "date": 1522389600000,
              "times": ["9:00 am", "2:30 pm", "5:00 pm", "6:00 pm"]
            },
            {
              "date": 1522476000000,
              "times": ["10:00 am", "2:30 pm", "5:00 pm"]
            },
            {
              "date": 1522735200000,
              "times": ["9:00 am", "2:30 pm", "5:00 pm", "6:00 pm"]
            }
          ]
        }
      ]
    ],
    ...
  ]

But I am stuck on how to go about this currently. I have probably been looking at this for too long to really wrap my head around this.

I have looked at the following places to get some ideas:

stack

stack

youtube

Any help would be appreciated!

1 Answer 1

0

This function

createCalendar(date, people) {
    const calendar = {};
    date.forEach(element => {
      calendar[element] = [];
      people.forEach(person => {
        if (person.availability.map(item => item.date).includes(element)) {
          calendar[element].push(person);
        }
      })
    })
    return calendar;
  }

creates a similar data structure to the one you had in mind. calendar is an object, where each key is a date, the corresponding value is an array containing all the people with availability for that date.

Sign up to request clarification or add additional context in comments.

3 Comments

Awesome! That was what I was looking for here... thanks!
Do you know if there would be a way to make it more like an array of arrays? Like : let calendar = [ [ "date": 1522303200000, "photographers": [...] ], ["date":... I think that was really how I am thinking about this. Thanks for all your help!
Well it certainly is possible to make it in the form of an array of arrays, but the syntax you are proposing is not valid. You don't have key-value pairs in an array, so you can't have something like [ 'date': 123]. If you want key-value pairs (which I think makes sense) you need objects.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.