forEach is not a function error with JavaScript array

First option: invoke forEach indirectly The parent.children is an Array like object. Use the following solution: const parent = this.el.parentElement; Array.prototype.forEach.call(parent.children, child => { console.log(child) }); The parent.children is NodeList type, which is an Array like object because: It contains the length property, which indicates the number of nodes Each node is a property value … Read more

Is there a mechanism to loop x times in ES6 (ECMAScript 6) without mutable variables?

Using the ES2015 Spread operator: […Array(n)].map() const res = […Array(10)].map((_, i) => { return i * 10; }); // as a one liner const res = […Array(10)].map((_, i) => i * 10); Or if you don’t need the result: […Array(10)].forEach((_, i) => { console.log(i); }); // as a one liner […Array(10)].forEach((_, i) => console.log(i)); Or … Read more

How can I find and update values in an array of objects?

You can use findIndex to find the index in the array of the object and replace it as required: var item = {…} var items = [{id:2}, {id:2}, {id:2}]; var foundIndex = items.findIndex(x => x.id == item.id); items[foundIndex] = item; This assumes unique IDs. If your IDs are duplicated (as in your example), it’s probably … Read more

Unable to access React instance (this) inside event handler [duplicate]

this.changeContent needs to be bound to the component instance via this.changeContent.bind(this) before being passed as the onChange prop, otherwise the this variable in the body of the function will not refer to the component instance but to window. See Function::bind. When using React.createClass instead of ES6 classes, every non-lifecycle method defined on a component is … Read more

Concatenating variables and strings in React

You’re almost correct, just misplaced a few quotes. Wrapping the whole thing in regular quotes will literally give you the string #demo + {this.state.id} – you need to indicate which are variables and which are string literals. Since anything inside {} is an inline JSX expression, you can do: href={“#demo” + this.state.id} This will use … Read more

What is “function*” in JavaScript?

It’s a Generator function. Generators are functions which can be exited and later re-entered. Their context (variable bindings) will be saved across re-entrances. Calling a generator function does not execute its body immediately; an iterator object for the function is returned instead. When the iterator’s next() method is called, the generator function’s body is executed … Read more

Why es6 react component works only with “export default”?

Exporting without default means it’s a “named export”. You can have multiple named exports in a single file. So if you do this, class Template {} class AnotherTemplate {} export { Template, AnotherTemplate } then you have to import these exports using their exact names. So to use these components in another file you’d have … Read more