For循环仅输出最后一个元素

时间:2016-02-15 09:33:49

标签: javascript for-loop

很抱歉,如果重复但似乎无法在任何地方找到正确的答案: 在console.log中,输出是正确的,但在屏幕上它只显示数组的最后一个元素

    {id: 3, naam: "Besured", type: "Voor alle pakketten geldt:, Ready-2-go ,Love-2-move, Alles-in-1 ", vergoeding:"Registratie bij koepel vereist , Geen vergoeding, Geen vergoeding, 80% tot € 200 per jaar maximaal € 50 per consult"},

            var string = test[i].type;
            var thisString = string.split(",");


        for(var j=0; j<thisString.length;j++){
                console.log(thisString[j]);
                $('.first')[0].innerHTML = thisString[j];
                $('.second')[0].innerHTML = thisString[j];
                $('.third')[0].innerHTML = thisString[j];
                $('.fourth')[0].innerHTML = thisString[j];
            }

1 个答案:

答案 0 :(得分:0)

您的评论说

  

我需要在第一个类的数组的第一个元素中,第二个   第二类等项目

然后尝试

    $('.first').html( trimStr( thisString[0] ) ); 
    $('.second').html( trimStr( thisString[1] ) );
    $('.third').html( trimStr( thisString[2] ) );
    $('.fourth').html( trimStr( thisString[3] ) );

function trimStr( input ) {

    if (typeof input === 'undefined' || input == null) 
    {
        input =  "";
    }

    return input.trim();
}
相关问题