React: .map is undefined when json object is passed to it

Created on 13 Feb 2014  路  4Comments  路  Source: facebook/react

In my reactjs library I'm using underscore.js right now see here, because the .map function doesn't work on a json object.

The data being passed using .map consists of this json schema, (here's an excerpt)

var json = {
        "description": null, 
        "title": "Site Detail",
        "ux-submit-text" : "submit form",
        "href": "/api/v2/sites/{site_id}/", 
        "rel": "update", 
        "method": "PUT", 
        "schema": {
            "type": "object", 
            "properties": {
                "name": {
                    "type": "string", 
                    "description": "\n        Your Site Name will appear in the title bar of the browser. It tells\n        users and search engines what your site is about.\n    ", 
                    "title": "Site name"
                }
            }
        }
    };

I would like to use the built in .map function and not rely on an external library. What is wrong here?

Most helpful comment

Objects don't have a map function. You can iterate through an object like so:

var mapped = {};
for (var key in obj) {
  if (!obj.hasOwnProperty(key)) {
    continue;
  }
  var val = obj[key];
  mapped[key] = ...;
}

All 4 comments

Objects don't have a map function. You can iterate through an object like so:

var mapped = {};
for (var key in obj) {
  if (!obj.hasOwnProperty(key)) {
    continue;
  }
  var val = obj[key];
  mapped[key] = ...;
}

It would help if react's map function converted it for you the way that underscore does it, because this way, it doesn't convert nested elements.

React doesn't provide a .map function; it's part of the JavaScript standard and is provided by the browser.

Ah, okay I thought that may have been the case. Would be useful though. Thanks for the info.

Was this page helpful?
0 / 5 - 0 ratings