0

So I have an array of objects userEvents that I want to use the event array inside of the object. The event array is storing objects as well, in which I am trying to access the start property and change it. I was thinking about nesting map functions, but I have had no success. I attached the console log of array of userEvents enter image description here

const { events } = this.props.events;
    const userEvents = events
3
  • please add the data in text form to the question. please have a look, too: minimal reproducible example Commented Aug 4, 2018 at 16:56
  • FYI you can shorten your current code to const { events: userEvents } = this.props.events Commented Aug 4, 2018 at 17:01
  • @Li357 How would I shorten it further by taking the events array inside userEvents? Commented Aug 4, 2018 at 18:16

2 Answers 2

1

Giving you just want

access start property

userEvents.forEach(userEvent => userEvent.events
  .forEach(event => { console.log(event.start) })
)
Sign up to request clarification or add additional context in comments.

Comments

0

You have an array that basically simplifies to this:

let userEvents = [
  {events: [{start: 1},{start: 2}]},
  {events: [{start: 3},{start: 4}]}
]

You should be able to loop through userEvents, then inside that, look at each element's events array and loop through that:

let userEvents = [
  {events: [{start: 1},{start: 2}]},
  {events: [{start: 3},{start: 4}]}
]

userEvents.forEach(e => {
  e.events.forEach(event =>{
    console.log(event.start)
  })
})

This will get you access to the elements you want. If you are trying to extract these or turn them into a different data format, of course tools like map(), filter(), and reduces() will be helpful.

For example, getting all your starts as an array:

let userEvents = [
  {events: [{start: 1},{start: 2}]},
  {events: [{start: 3},{start: 4}]}
]

let starts = userEvents.reduce((a, e) => {
  return [...a, ...e.events.map(event => event.start)]
}, [])
console.log(starts)

Comments

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.