Nevin
Nevin

Reputation: 3298

Conversion of for loops to "for each"

Have been stuck on this for a while: I tried converting the code below to for each statements,and i ended up with errors.

ChartClass.prototype.dataTranslatorLine = function(data) {
        jsonLength = Object.keys(data).length;
        for (j = 0; j < jsonLength; j += 2) {
            var innerObjArray = new Array();
            positionNumber = Object.keys(data[j].position).length;
            for (k = 0; k < positionNumber; k++) {
                var obj = {};
                obj.x = data[j].position[k];
                obj.y = data[j + 1].position[k];
                innerObjArray.push(obj);
            }
            dataArray.push(innerObjArray);
        }
        return dataArray;
    };

Can anyone help me out with this?

Check out my fiddle here

Upvotes: 0

Views: 140

Answers (1)

zbrunson
zbrunson

Reputation: 1757

I'm not entirely sure what is going on, but this should be a pretty direct translation to using forEach.

ChartClass.prototype.dataTranslatorLine = function(data) {
    var dataArray = [];
    Object.keys(data).forEach(function(key, idx) {
        if (idx % 2 === 1) {
            return;
        }

        var innerObjArray = [];
        Object.keys(data[idx].position).forEach(function(key2, idx2) {
            var obj = {
                x: data[idx].position[idx2],
                y: data[idx + 1].position[idx2]
            };
            innerObjArray.push(obj);
        });

        dataArray.push(innerObjArray);
    });

    return dataArray;
};

A couple of notes though: if data is an array, there is no need to call Object.keys on it, just go directly for the iteration; this code is rather convoluted, and I would think that with some work on the data structure being passed in could make more sense; and a for loop may be better for you situation instead of the forEach loop since you are primarily working on index instead of doing stuff just with the values.

EDIT:

After looking at your data structure this is a quick and dirty way to do it, but I still suggest you rework how you are storing your data into something that makes more sense.

ChartClass.prototype.dataTranslatorLine = function(data) {
    for (var i = 0; i < data.length; i += 2) {
        x = data[i].position;
        y = data[i + 1].position;

        var innerObj = [];
        for (var j = 0; j < x.length; j++) {
            innerObjArray.push({
                x: x[j],
                y: y[j]
            });
        }

        dataArray.push(innerObj);
    }

    return dataArray;
};

The forEach doesn't buy you anything since you are working with indexes, not just the contents of the array. As for what key is in Object.keys(data).forEach(function(key, idx) { for you it will be the strings 'name' and 'position' since you are iterating over the keys of the object. Also, if (idx % 2 === 1) { return; } is how it is mimicking the j += 2 from your original for loop, basically exiting the function if it is an odd index.

Upvotes: 1

Related Questions