Object.keys
I adore JavaScript objects. Love them. You're probably asking "well, why don't you marry them?" Trust me: if I could, I would. Arrays are nice and all but object keys provide another level of structure and information that is invaluable. For example, it's much faster search an object for a key than it is to search an array for value presence.
The way we've always iterated on an Object
instance was always a for
loops with a hasOwnProperty
check which was ugly; Object.keys
(not Object.prototype.keys
) provides an array of Object properties!
var person = {
firstName: 'David',
lastName: 'Walsh',
// ...
};
Object.keys(person).forEach(function(trait) {
console.log('Person ', trait,': ', person[trait]);
});
If you work with JSON or simply raw JavaScript objects, and you haven't been using Object.keys
, now is the time to ditch the old method for this elegant solution!
Introduction
For quite a long time now websites with the so called "parallax" effect have been really popular.
In case you have not heard of this effect, it basically includes different layers of images that are moving in different directions or with different speed. This leads to a...
CSS animations are a lot of fun; the beauty of them is that through many simple properties, you can create anything from an elegant fade in to a WTF-Pixar-would-be-proud effect. One CSS effect somewhere in between is the CSS flip effect, whereby there's...
A week back I saw a great effect created by CSSKarma: input labels being animated horizontally. The idea is everything positive: elegant, practical, unobtrusive, and requires very little jQuery code. Luckily the effect doesn't require much MooTools code either!
The HTML
A...
CSS is becoming more and more powerful but in the sense that it allows us to do the little things easily. There have been larger features added like transitions, animations, and transforms, but one feature that goes under the radar is generated content. You saw a...
Why don’t you use this:
@Franz,
for..in iterating over NON own properties.
Not very well supported http://caniuse.com/#feat=let
Obligatory https://babeljs.io/
Also would iterate over prototype properties.
The
for each...in
statement is deprecated as the part of ECMA-357 (E4X) standard. E4X support has been removed, butfor each...in
will not be disabled and removed because of backward compatibility considerations. Consider usingfor...of
instead. (Please refer to bug 791343.)Also: Use
const
, not let. The loop creates a new context in each iteration, the loop variable therefore is constant unless your loop-code changes it.