Does “for…of” loop iteration follow the array order in JavaScript?

Iterating over an array using for…in doesn’t guarantee order, however ES6 introduces a new construct for…of. My limited testing of implementations of for…of indicates that it does iterate in order on array, but is this property guaranteed? Yes, the order of for-of on arrays is guaranteed by the array iterator definition: It will visit the … Read more

Updating React state when state is an array of objects

Your update function would look like this updateItem(id, itemAttributes) { var index = this.state.items.findIndex(x=> x.id === id); if (index === -1) // handle error else this.setState({ items: [ …this.state.items.slice(0,index), Object.assign({}, this.state.items[index], itemAttributes), …this.state.items.slice(index+1) ] }); } And you use it like this this.updateItem(2, {someattr: ‘a new value’}); Gross right? You’re going to have a big … Read more

Importing node modules from root directory using es6 and babel-node

Like (almost) any tool ‘/x’ means ‘x’ in the root of your filesystem. Babel doesn’t actually look at the paths, it just compiles import {myFunc} from ‘/my-module’; into var _myModule = require(‘/my-module’); And node actually looks up the module. If you really want to import relative to the root of the project, you could use … Read more

Export more than one variable in ES6?

That is not valid syntax. You can do export { Post } or even just export var Post = Parse.Object.extend(‘Post’) or shorten the whole file to export default Parse.Object.extend(‘TestObject’) export var Post = Parse.Object.extend(‘Post’) Your imports are also incorrect, you’ll want to do import TestObject, { Post } from ‘../store’ This is if you really … Read more

This.props.dispatch not a function – React-Redux

Per the Redux FAQ question at here, this.props.dispatch is available by default if you do not supply your own mapDispatchToProps function. If you do supply a mapDispatchToProps function, you are responsible for returning a prop named dispatch yourself: const mapDispatchToProps = dispatch => ({ …other methods, dispatch // ← Add this }) export default connect(null, … Read more

Expected to return a value in arrow; function array-callback-return. Why?

Your specific example is: data.map(users => { console.log(users); }); Where data is the following array: [ {id: 1, username: “Foo”}, {id: 2, username: “Bar”}, ] data.map function (check Array.prototype.map specification) converts one array (data in your case) to a new array. The conversion (mapping) is defined by the argument of data.map, also called the callback … Read more

Why is the response object from JavaScript fetch API a promise?

If your question is “why does response.json() return a promise?” then @Bergi provides the clue in comments: “it waits for the body to load”. If your question is “why isn’t response.json an attribute?”, then that would have required fetch to delay returning its response until the body had loaded, which might be OK for some, … Read more