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!
![Animated 3D Flipping Menu with CSS]()
CSS animations aren't just for basic fades or sliding elements anymore -- CSS animations are capable of much more. I've showed you how you can create an exploding logo (applied with JavaScript, but all animation is CSS), an animated Photo Stack, a sweet...
![Creating Scrolling Parallax Effects with CSS]()
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...
![Image Data URIs with PHP]()
If you troll page markup like me, you've no doubt seen the use of data URI's within image src attributes. Instead of providing a traditional address to the image, the image file data is base64-encoded and stuffed within the src attribute. Doing so saves...
![Create Spinning, Fading Icons with CSS3 and MooTools]()
A goal of my latest blog redesign was to practice what I preached a bit more; add a bit more subtle flair. One of the ways I accomplished that was by using CSS3 animations to change the display of my profile icons (RSS, GitHub, etc.) I...
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.