简体   繁体   中英

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)
        }
    }

Change

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

to

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM