UI Architect, Full Stack Web Developer – San Francisco Bay Area (Walnut Creek), CA
UI Architect, Full Stack Web Developer – San Francisco Bay Area (Walnut Creek), CA

New Javascript ForEach iterator

Have to start getting used to some iterator syntax.  Here’s how we USED to do it:

 
for (let color in colors) {
            let colorObj = {};

            colorObj.code = colors[color].colorPrdId;
            colorObj.name = colors[color].colorName;
            colorObj.imgSwatch = colors[color].imgSwatch;

            collection.push(colorObj);
        }

Here’s the new way to code the same thing:

        colors.forEach(function (color) {
            collection.push({
                code: color.colorPrdId,
                name: color.colorName,
                imgSwatch: color.imgSwatch
            });
        });

More about modern iterators

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.