Manipulating Array Elements During Mapping in React

If only they had some kind of identification

Problem:

The Example:

That’s an excellent API, btw, very helpful and fun for training. Here’s a version I made 6 months ago in PHP. I just checked and the API was down so neither example was working.

…but we don’t need ’em to for this…

As you know, the records come through like this:

{
"name": "Luke Skywalker",
"height": "172",
"mass": "77",
"hair_color": "blond",
"skin_color": "fair",
"eye_color": "blue",
"birth_year": "19BBY",
"gender": "male",
"homeworld": "http://swapi.co/api/planets/1/",
"films": [
"http://swapi.co/api/films/2/",
"http://swapi.co/api/films/6/",
"http://swapi.co/api/films/3/",
"http://swapi.co/api/films/1/",
"http://swapi.co/api/films/7/"
],
"species": [
"http://swapi.co/api/species/1/"
],
"vehicles": [
"http://swapi.co/api/vehicles/14/",
"http://swapi.co/api/vehicles/30/"
],
"starships": [
"http://swapi.co/api/starships/12/",
"http://swapi.co/api/starships/22/"
],
"created": "2014-12-09T13:50:51.644000Z",
"edited": "2014-12-20T21:17:56.891000Z",
"url": "http://swapi.co/api/people/1/"
}

You can see the record doesn’t possess its own ID field. However, it does have a ‘url’ field, and tacked on to the url at the very end of the field is a number that you and I can tell is an ID number!

What We’re Tryna Do:

But we need to get the id of the character so we can pull it up in our own app. That ‘url’ field just provides a link to that character’s data, we want just the id, so we can call a page in our app like “showCharacter/1” and it’ll call for the character data then.

How We Been Doing It:

INB4 “them aren’t people” — I told you the API was down right now

And we mapped them like this:

{this.props.ship.people && this.props.ship.people.map(person => <Link to={{ "pathname": `/people/` }}> 
<span>{person.name}</span>
<img src={`${person.imgPath}`} />
</Link>
)}

You can see there where the “pathname” is set to /people which means that no matter which of those little cards the user clicks they’re always going to end up back on this page.

That’s because we don’t have a specific ID to send to another page to just display that one user.

What We Shoulda Had Been Doing:

…gadzooks…

Like this:

{this.props.ship.people && this.props.ship.people.map(person=> {let idStr = person.url.split('/');
let personID = idStr[5];
return (<Link to={{ "pathname": `/people/${personID}` }}>
<img src={`${person.imgPath'}`}/>
</Link>
)})}

And all I really did was add a curly brace and a “return(“ in front of it and a “)}” at the end.

Then I could use the space between the opening curly brace and the “return” keyword to do whatever machinations I wanted.

In this case, I took the url of each record as I came to it, then I split it up into an array and the sixth item in the array [5] was the one with the ID!

Now you notice how when they click on it they go someplace cool this time?

<Link to={{ "pathname": `/people/${personID}` }}>

That’ll take them to a specific page that can use that ID number to get the character’s specific details.

That’s All. Go Home.

--

--

4a 75 73 74 20 61 6e 6f 74 68 65 72 20 63 6f 6d 70 75 74 65 72 20 6e 65 72 64 20 77 69 74 68 20 61 20 62 6c 6f 67

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
crashdaddy

4a 75 73 74 20 61 6e 6f 74 68 65 72 20 63 6f 6d 70 75 74 65 72 20 6e 65 72 64 20 77 69 74 68 20 61 20 62 6c 6f 67