为什么此循环重复输出数据?

时间:2018-08-09 19:29:56

标签: javascript node.js loops

因此,即使我尝试将长度设置为变量并在内部循环中更改它,该循环仍会重复输出。

这是我的代码:

let testData = {
    'title': ['Dark knight', 'Monty python the holy grail', 'the social network'],
    'description': ['Batman yelling', 'Random stuff', 'facebook stuff']
};

generateFile(testData);

function generateFile(testData){


    for (let i =0; i < testData.title.length; i++){
        title = testData.title[i]
        for (let j = 0; j < testData.description.length; j++){
            description = testData.description[j];
            console.log(title);
            console.log(description);


        }

    }
}

这是输出:

Dark knight
Batman yelling
Dark knight
Random stuff
Dark knight
facebook stuff
Monty python the holy grail
Batman yelling
Monty python the holy grail
Random stuff
Monty python the holy grail
facebook stuff
the social network
Batman yelling
the social network
Random stuff
the social network
facebook stuff

它应该只输出一次。

3 个答案:

答案 0 :(得分:2)

您在其中有一个嵌套的for循环

for (var i=0; i<3; i++){
    for (var j=0; j<3; j++){
        //inside two loops
    }
}

因此,外循环运行3次,但内循环对每个外循环运行3次。 您可以采取的措施来解决此问题,或者是像这样的单个循环

for (let i=0; i<testData.titles.length; i++){
    console.log(testData.title[i]);
    console.log(testData.description[i]);
}

但是更好的选择可能是将数据重组为对象,因为数组是相关的。

let testData=[
    {
        title: 'Dark knight',
        description: 'batman yelling'
    }, ...
];

然后做

for (let i=0; i<testData.length; i++){
    console.log(testData[i].title);
    console.log(testData[i].description);
}

答案 1 :(得分:1)

您拥有的代码执行以下操作:

对于testData.title中的每个元素,它遍历testData.description中的每个元素,并输出title + description的组合。

由于您是嵌套循环,所以这是预期的。

如果您希望每个标题仅显示一个描述,则必须直接选择它。例如:

let testData = {
    'title': ['Dark knight', 'Monty python the holy grail', 'the social network'],
    'description': ['Batman yelling', 'Random stuff', 'facebook stuff']
};

generateFile(testData);

function generateFile(testData){


    for (let i =0; i < testData.title.length; i++){
        title = testData.title[i]
        description = testData.description[i];
        console.log(title);
        console.log(description);
    }

}

这将输出:

Dark knight
Batman yelling
Monty python the holy grail
Random stuff
the social network
facebook stuff

它将testData.title的元素0与testData.description的元素0组合在一起。

答案 2 :(得分:0)

对于嵌套循环,对于外循环的每次迭代,内循环将完全运行。我相信您想并行地遍历两个数组,这只需要一个循环:

function generateFile(testData) {
  for (let i = 0; i < testData.title.length; i++) {
    console.log(`${testData.title[i]}: ${testData.description[i]}`);
  }
}

let testData = {
    'title': ['Dark knight', 'Monty python the holy grail', 'the social network'],
    'description': ['Batman yelling', 'Random stuff', 'facebook stuff']
};

generateFile(testData);

请注意,您可能希望将两个数组组合成一个对象数组,每个对象都有一个标题和描述键值对。这是一种方法:

let testData = {
    'title': ['Dark knight', 'Monty python the holy grail', 'the social network'],
    'description': ['Batman yelling', 'Random stuff', 'facebook stuff']
};

const result = testData.title.map((e, i) => {
  return {title: e, description: testData.description[i]};
});

console.log(result);