texas697
texas697

Reputation: 6387

how to loop through array with javascript

I its basic but I am new to javascript. I am trying to loop through the array and match the objects that == my key.

this is what i am using right now, it works but i am only matching the first object that matches, sometimes there will be multiple objects that match.

Here is what i have now

var chartSeries = chartService.getSeries();
var marker.options.subdivision.id = 1345
var matchingSeries = Enumerable.From(chartSeries).Where('x => x.id == "' + marker.options.subdivision.id + '"').ToArray();
   var series = {
        id: matchingSeries[0].id,
        name: matchingSeries[0].name,
        data: matchingSeries[0].data,
        lineWidth: 5
    };

I need to include a for loop to match all objects.

    var subIdSeries = [];
    var subId = marker.options.subdivision.id;
    var series = {
        id: matchingSeries[0].id,
        name: matchingSeries[0].name,
        data: matchingSeries[0].data,
        lineWidth: 5
    };

    for (var i = 0; i < chartSeries.length; i++) {

        if (subId == chartSeries.id) {
            push.subIdSeries(subId)
        }
    }

Upvotes: 1

Views: 60

Answers (2)

Caner Akdeniz
Caner Akdeniz

Reputation: 1862

Without seeing the whole script, from what you have so far, I can suggest:

if (subId == chartSeries[i].id) {
    subIdSeries.push(subId)
}

Upvotes: 2

Frank
Frank

Reputation: 664

Change

if (subId == chartSeries.id) {
    push.subIdSeries(subId)
}

to

if (subId == chartSeries[i].id) {
    subIdSeries.push(subId)
}

Upvotes: 3

Related Questions