如何停止循环只获取第一个数组的内容?

时间:2017-03-16 14:25:58

标签: javascript jquery html arrays for-loop

这是我的代码的模型。

我正在接收此变量中的数据:

this.data = data.details;

然后我正在过滤要删除的重复项并将其放入另一个数组中:

let dataUnique = this.data.filter(function(el, index, self) {
     return index == self.indexOf(el);
}

console.log的{​​{1}}:

dataUnique

现在问题发生在这个if语句中:

Array[14]
0: "a"
1: "b"
2: "c"
3: "d"
4: "e"
5: "g"
6: "h"
7: "i"
8: "j"
9: "k"
10: "l"
11: "m"
12: "n"
13: "o"

问题在于,我if(something == true) { let uData = JSON.parse(localStorage.getItem('user')); switch (uData.language) { case 'de': document.querySelector('table tr').insertAdjacentHTML('afterbegin', '<td'> + dataUnique + '</td>'); break; } } 字段的tds数量等于我dataUnique arraysomething true的数量if switch之前的陈述。

所以不要得到:

<table>
    <tr>
        <td>a b c d e f g h i j k l m n o</td>
    </td>
</table>

我收到了:

<table>
    <tr>
        <td>a b c d e f g h i j k l m n o</td>
        <td>a b c d e f g h i j k l m n o</td>
        <td>a b c d e f g h i j k l m n o</td>
        <td>a b c d e f g h i j k l m n o</td>
        <td>a b c d e f g h i j k l m n o</td>
    </td>
</table>

<td>a b c d e f g h i j k l m n o</td>的数量等于something == true声明中的数字if。我知道问题是什么,但我不知道如何解决它。

感谢。

编辑:我在上面循环,即:

for (let sth in somethingObj) {
     let something = somethingObj[sth];
     if(something.something == true) {...}
}

2 个答案:

答案 0 :(得分:2)

javascript允许您命名循环(如EMCA-262第12.12节中所定义。[MDN Docs])。

这允许我们为break命令指定循环名称,以便js突破THAT循环:

loop1:
for(var i = 0; i < 10; i++) {
loop2:
  for(var j=0;j<10;j++) {

    console.log(i, j);
    break loop1;

  }
}

这里我们告诉break命令突破loop1console.log中的结果为0 0。如果我们刚刚休息,我们最终会得到类似的结果:

0 0
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0

将此逻辑添加到您的代码中:

outerLoop:
for (let sth in somethingObj) {
     let something = somethingObj[sth];
     if(something == true) {
        let uData = JSON.parse(localStorage.getItem('user'));
        switch (uData.language) {
             case 'de':
             document.querySelector('table tr').insertAdjacentHTML('afterbegin', '<td'> + dataUnique + '</td>');
             break outerLoop;
        }
    }
}

答案 1 :(得分:1)

将检查放在循环中当然会为td提供truebreakswitchswitch内的for就在something.something内仅适用于var test = false; for (let sth in somethingObj) { let something = somethingObj[sth]; if(something.something == true) {test=true;break ;} } if(test){//do your if business here. let uData = JSON.parse(localStorage.getItem('user')); switch (uData.language) { case 'de': document.querySelector('table tr').insertAdjacentHTML('afterbegin', '<td'> + dataUnique + '</td>'); break; } } 而不适用于for (let sth in somethingObj) { let something = somethingObj[sth]; if(something.something == true) { let uData = JSON.parse(localStorage.getItem('user')); switch (uData.language) { case 'de': document.querySelector('table tr').insertAdjacentHTML('afterbegin', '<td'> + dataUnique + '</td>'); break; // this breaks out of the switch .. } break; // this breaks out of the for loop } } 循环...我说评估hs_err_pid7072.log并将其分配给变量,然后执行下一步:< / p>

private async void OnItemSelected(object sender, SelectedItemChangedEventArgs e)
{
    await ShowDetailView();
}

private async Task ShowDetailView()
{
    var view = new MyView();
    this.detailView = view;
    await view.LoadContent();
}

另一种方式(对逻辑进行最少的编辑):

public async Task LoadContent()
{
    var success = await LoadData();
    if (success)
    {
        ShowInformation();
    }
}

public async Task<bool> LoadData()
{
    try
    {
        ShowLoadingProcess();
        await Task.Delay(5000);
        this.itemList = await WebService.Instance.GetData();

        return true;
    }
    catch (Exception ex)
    {
        System.Diagnostics.Debug.WriteLine(ex.Message);
        return false;
    }
    finally
    {
        HideLoadingProcess();
    }
}

private void ShowInformation()
{
    Device.BeginInvokeOnMainThread(() =>
    {
        this.grid.Clear();

        foreach(var item in this.itemList)
        {
            GridItem contentItem = new GridItem(item);
            this.grid.Children.Add(contentItem);
        }
    }
}

private void ShowLoadingProcess()
{
    Device.BeginInvokeOnMainThread(() => BringProgressIndicatorToFront());
}

private void HideLoadingProcess()
{
    Device.BeginInvokeOnMainThread(() => BringProgressIndicatorToBack());
}