node.js - How to work with JSON object in React -
so here set up:
i have simple nodejs backend (based on express , mongoose), responds request json (in form of object of objects).
so after response, want render component, each elements of said object of objects. if array, use array.map(), , render component in callback function. since have, object, can not use that.
so... should return , array backend. if how tell mongoose return result of model.find() in form of array.
or should convert object array in frontend? in case, how without putting through loop of sort?
lastly, tried make work so:
render: function() { //console.log('render todolist componenr'); var items = this.state.todoitems; return( <ul> {for (var item in items){ console.log(item); }} </ul> ); }
to error:
uncaught syntaxerror: embedded: unexpected token (30:9) 28 | return( 29 | <ul> > 30 | {for (var item in items){ | ^ 31 | 32 | }} 33 | </ul>
which super weird, points empty location?
any ideas how make work?
to iterate on object use object.keys so:
object.keys(yourobject).map(function(key) { return renderitem(yourobject[key]); });
the method returns array of given object's own enumerable properties, in same order provided for...in loop (the difference being for-in loop enumerates properties in prototype chain well).
it's supported ie >= 9, chrome >= 5, safari >= 5, firefox >= 4.
Comments
Post a Comment