Using forEach on an array from getElementsByClassName results in “TypeError: undefined is not a function”

That’s because document.getElementsByClassName returns a HTMLCollection, not an array. Fortunately it’s an “array-like” object (which explains why it’s logged as if it was an object and why you can iterate with a standard for loop), so you can do this : [].forEach.call(document.getElementsByClassName(‘myClass’), function(v,i,a) { With ES6 (on modern browsers or with Babel), you may also … Read more

c# foreach (property in object)… Is there a simple way of doing this?

Give this a try: foreach (PropertyInfo propertyInfo in obj.GetType().GetProperties()) { // do stuff here } Also please note that Type.GetProperties() has an overload which accepts a set of binding flags so you can filter out properties on a different criteria like accessibility level, see MSDN for more details: Type.GetProperties Method (BindingFlags) Last but not least … Read more

Java 8 forEach with index [duplicate]

Since you are iterating over an indexable collection (lists, etc.), I presume that you can then just iterate with the indices of the elements: IntStream.range(0, params.size()) .forEach(idx -> query.bind( idx, params.get(idx) ) ) ; The resulting code is similar to iterating a list with the classic i++-style for loop, except with easier parallelizability (assuming, of … Read more

Advantages of std::for_each over for loop

The nice thing with C++11 (previously called C++0x), is that this tiresome debate will be settled. I mean, no one in their right mind, who wants to iterate over a whole collection, will still use this for(auto it = collection.begin(); it != collection.end() ; ++it) { foo(*it); } Or this for_each(collection.begin(), collection.end(), [](Element& e) { … Read more

PHP Error : Unparenthesized `a ? b : c ? d : e` is deprecated. Use either `(a ? b : c) ? d : e` or `a ? b : (c ? d : e)` [duplicate]

You need to add parentheses around your code: Before: $reference->frotel_vitrine = empty($item->special) ? null : $item->special == 2 || $item->special == 3 ? ‘active’ : ‘deactivate’; After : $reference->frotel_vitrine = empty($item->special) ? null : (($item->special == 2 || $item->special == 3 )? ‘active’ : ‘deactivate’); That should solve the issue.