3
const normalizeEventTypes = nextProps.events.space_events.map(obj, i => 
     obj.id
)

I code obj is not defined, my array of object look like this

{
    "space_events": [{
            "id": 1,
            "name": "Anniversaries"
        },
        {
            "id": 2,
            "name": "Brand Rollout"
        }
    }]
}

am I missing something?

1
  • 2
    Any errors in the console? Note @MayankShukla below. Make sure to accept it, if the problem is indeed resolved. Commented May 14, 2017 at 16:09

1 Answer 1

5

You forgot to use (), write it like this:

const normalizeEventTypes = nextProps.events.space_events.map((obj, i) => 
     obj.id
)

Reason:

You are using obj and index both parameters in map callback function so you need to use () to wrap the parameters, like this:

a = b.map((i,j) => i)

These () are optional when we want to use only one parameter, like this:

a = b.map(i => i)

Different ways of using map:

1. a.map(i => i + 1); //when index is not required

2.

 a.map(i => {  //when want to do some calculation
       //some calculation
       return //something;
 })

3. a.map((i,j) => i + j) //when want to use the index of item

Check the working snippet:

let data = {
    "space_events": [{
            "id": 1,
            "name": "Anniversaries"
        },
        {
            "id": 2,
            "name": "Brand Rollout"
        }
    ]
}

let result = data.space_events.map((obj,i) => obj.name);

console.log('result', result);

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

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.