0

Basically, I have an array of posts that contains 3 objects: posts[{},{},{}]

Each object looks something like this:

1: Object
__v: 0
_id: "5686e17fc64feafd4486f22c"
created_at: "2016-01-01T20:28:47.889Z"
image: "Test"
message: "Test"
title: "Test Post 1"
updated_at: "2016-01-01T20:28:47.889Z"

I'm wondering how I can get an index or something of each object, but also use that for the object's properties.

I was testing earlier with an object of objects, and all I had to do there was: Object.keys(this.props.data).map(this.renderPostTile). Then I could just use a parameter for renderPostTile(key).

How can I do something similar when I have an array of objects?

enter image description here

10
  • Thanks. However, how can I do something similar to Object.keys(data).map(someFunction)? I am trying to render a <p> with the titles, but not sure how I can do that. Commented Jan 2, 2016 at 21:48
  • posts.map(function(post) {...}) Commented Jan 2, 2016 at 21:50
  • Unfortunately, when I do that, it says .map is not a function. Here's what I am doing: {postData.map(this.renderTitle)} Above I have defined postData as this.props.data. Commented Jan 2, 2016 at 21:53
  • Why are you calling it on "postData" when you say your array is named "posts"? And why are you wrapping it with curly brackets? It's just a function call. posts.map(this.renderTitle); Commented Jan 2, 2016 at 22:03
  • sorry I have a var postData = this.props.posts sorry for the confusion Commented Jan 2, 2016 at 22:05

2 Answers 2

2

Like this:

var posts = [
  { title: "Test Post 1" },
  { title: "Test Post 2" },
  { title: "Test Post 3" }
];

posts.map(function(post){
  var p = document.createElement("p"),
      t = document.createTextNode(post.title);
  p.appendChild(t);
  document.body.appendChild(p);
});

Obviously your objects are more complicated, but you get the idea.

Edit:

React-wise, you could do something like this:

var PostListRender = React.createClass({
  render: function() {
    return (
      <ul>
        {this.props.posts.map(function(post){
          return <li>{post.title}</li>;
        })}
      </ul>
    )
  }
});

var posts = [
  {title: "Test Post 1"}, 
  {title: "Test Post 2"}, 
  {title: "Test Post 3"}
];

var el = document.getElementById('post-container');
React.render( < PostListRender posts = {posts} />, el);

Demo

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

4 Comments

Thanks Jack. However, here is the issue. I am doing the following: renderPostTile(key) { return ( <PostTile key={key} index={key} postData={this.props.posts[key]} /> ); } render() { var postData = this.props.posts; return ( <div className="container"> <div className="row"> {postData.map(this.renderPostTile)} </div> </div> ); } Unfortunately, I get an error saying TypeError: postData.map is not a function
oh god. how do I edit the code so it looks nice in the comment? here it is in a pastebin. pastebin.com/hdiq9k4t
looks like you're just getting postData and posts mixed up, but there are other issues there. You're going to have to update the question as the question you've posed has been answered, if you still have problems they are different ones.
Hi Jack. I'm not able to create another post for another 90 minutes. Basically, I think the issue is that on reload, this.props.posts is null again for some reason. That is leading to all these issues. Since its null, can't use .map(), and I can't even output the title, etc.
0

Object.keys returns an Array, that's why you can do .map() on it, which is an Array method. So yeah, you can utilize map on the Array. Literally just call posts.map(myCallback);

You're literally taking something where you converted an Object's keys INTO an Array for the purposes of using an Array method on it and asking "Can I do this to an Array too?" The answer is "well...of course, you just don't have to convert it to anything first...just call the Array's .map method".


You keep saying it says that postData.map isn't a function...but you say your Array is named posts...so it would be posts.map. If whatever you're working with doesn't have a map method, then it's not an Array (at least in modern browsers). Make sure you're calling it on the Array.

2 Comments

thanks. can you please check my first post? i updated with screenshot of console.log(this.props.post)
@user1354934 - I don't know react (you really should have stated that's what you were dealing with from the start). All I know is that a modern standards compliant JS engine will have a .map() method on any Array. If your "Array" doesn't have it then you should be double checking that you're actually referencing an Array in that spot. Keep in mind that this can refer to different things depending on the function caller...so check in that spot in the code whether what you're getting is actually the Array you expect or not.

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.