Javascript嵌套for循环显示来自JSON对象数组的数据

时间:2014-04-02 10:16:20

标签: javascript arrays json object

我从JSON文件中获取数据并使用javascript,因此我可以在网页上显示它。 json文件包含电影转换的数据。每部电影都有他们演奏的名字,身份和角色(有些人扮演多个角色)。

"abridged_cast":[
                            {"name":"Tom Hanks","id":"162655641","characters":["Woody"]},
                            {"name":"Tim Allen","id":"162655909","characters":["Buzz Lightyear"]},
                            {"name":"Joan Cusack","id":"162655020","characters":["Jessie the Cowgirl"]},
                            {"name":"Ned Beatty","id":"162672460","characters":["Lots-o'-Huggin' Bear","Lotso"]},
                            {"name":"Don Rickles","id":"341817905","characters":["Mr. Potato Head"]}
                            ],

我想在网页上显示强制转换,如下所示: 主演:Tom Hanks饰演Woody,Tim Allen饰演巴斯光年,Joan Cusack扮演Jessie the Cowgirl,Ned Beatty饰演Lots-o' -Huggin'熊& Lotso,Don Rickles饰演Potato Head先生。

我得到的是: 主演:Tom Hanks as& Woody,Tim Allen as& Buzz Lightyear,Joan Cusack as& Jessie the Cowgirl,Ned Beatty饰演Lots-o' -Huggin'熊,& Lotso,Don Rickles as& Potato Head先生,

我只想要"&"如果演员扮演多个角色,则在最后一个和最后一个角色之间播放。例如: 主演:约翰如虎,狮子,树&乌鸦。

我花了很多年的时间试图弄明白,我真的看不出我做错了什么。这是我的javascript:

$(document.body).append('<strong>Starring:</strong> ');
       for (var i = 0;i < movie.abridged_cast.length; i++){
            $(document.body).append(movie.abridged_cast[i].name + " as ");
            for (var j = 0; j < movie.abridged_cast[i].characters.length; j++){
                    if(movie.abridged_cast[i].characters[j] == movie.abridged_cast[i].characters[movie.abridged_cast[i].characters.length -1]
                    // if the character = the character in the last position
                        && movie.abridged_cast[i].characters[j].length -1 > 0){
                        // and the position of the character is greater than 0
                        $(document.body).append('& ' + movie.abridged_cast[i].characters[j] + ', ');
                    }
                    else {
                         $(document.body).append(movie.abridged_cast[i].characters[j] + ', ');
                    }
            }
       }

3 个答案:

答案 0 :(得分:0)

检查字符长度并使用索引而不是比较数据

for (var j = 0; j < movie.abridged_cast[i].characters.length; j++){
                // if more than 1 character
                if(movie.abridged_cast[i].characters.length>1
                // and it is the last character in array
                   && j == movie.abridged_cast[i].characters.length-1){
                    $(document.body).append('& ' + movie.abridged_cast[i].characters[j] + ', ');
                }
                else {
                     $(document.body).append(movie.abridged_cast[i].characters[j] + ', ');
                }
        }

答案 1 :(得分:0)

   for (var i = 0;i < movie.abridged_cast.length; i++){
       if ( movie.abridged_cast[i].characters.length > 0 ) {
            $(document.body).append(movie.abridged_cast[i].name + " as ");
            $(document.body).append(movie.abridged_cast[i].characters[0]);
            if ( movie.abridged_cast[i].characters.length > 1 ) {
                for (var j = 1; j < movie.abridged_cast[i].characters.length-1; j++){
                     $(document.body).append(', ' + movie.abridged_cast[i].characters[j]);
                }
                $(document.body).append(' & ' + movie.abridged_cast[i].characters[movie.abridged_cast[i].characters.length-1]);
            }
            $(document.body).append('<br>');
       }
   }

答案 2 :(得分:0)

我已经使用数组来连接字符串,因为我觉得它们更容易使用。在小提琴中的示例中,我添加了一个额外的名称来测试字符列表中有两个以上的名称。

var summary = ['Starring '];
for (var i = 0, l = data.length; i < l; i++) {
    var txt = [], name = data[i].name, chars = data[i].characters;
    txt.push(name + ' as ');
    if (chars.length > 2) {
      txt.push(chars.slice(0, chars.length - 1).join(', '));
      txt.push(' & ' + chars.pop());
    } else {
      txt.push(chars.join(', '));
    }
    txt = txt.join('');
    if (i < data.length - 1) {
      summary.push(txt + ', ');
    } else {
      summary.push(txt + '.');
    }
}

console.log(summary.join(''));

或者在你的情况下:

$(document.body).append(summary.join(''));

<强>输出

Tom Hanks饰演Woody,Tim Allen饰演巴斯光年,Joan Cusack扮演Jessie the Cowgirl, Ned Beatty饰演Lots-o&#39; -Huggin&#39; Bear,Lotso&amp; Boo-Boo ,Don Rickles饰演Potato Head先生。

Fiddle