返回指令停止我的JavaScript循环

时间:2019-06-12 19:13:16

标签: javascript for-loop

当我使用for循环时,

没问题
for (var h = 0; h < xt.length; h++) {
  var heart = '';
  console.log(xt[h]);
  if (row[0] == xt[h]) {
    heart = 'icon-heart';
  } else {
    heart = 'icon-heart1';
  }
  return '<i class="' + heart + '" onClick="addFAv(this)"  data-id="' + row[0] + '" data-name="' + row[1] + '"></i> <a href="#" class="icon-line-clipboard i-circled i-small"></a> <a href="#" class="icon-file-alt2 i-circled i-small"></a>';
}

但是当我使用return指令时,循环停止:

$('#recevabilite').dataTable({
      "scrollY": "200px",
      "scrollCollapse": true,
      "paging": false,
      stateSave: true,
      "dom": '<"#recev.top">irt',
      "columnDefs": [{
            "visible": false,
            "targets": [3]
          },
          {
            "render": function(data, type, row) {
                //    var date1 = new Date()+10J;
                //affichage des icones  
                var x = localStorage.getItem("listeFavoris");
                xt = x.split(',');

                for (var h = 0; h < xt.length; h++) {
                  var heart = '';
                  console.log(xt[h]);
                  if (row[0] == xt[h]) {
                    heart = 'icon-heart';
                  } else {
                    heart = 'icon-heart1';
                  }
                  return '<i class="' + heart + '" onClick="addFAv(this)"  data-id="' + row[0] + '" data-name="' + row[1] + '"></i> <a href="#" class="icon-line-clipboard i-circled i-small"></a> <a href="#" class="icon-file-alt2 i-circled i-small"></a>';
                }

你知道为什么吗?

函数中的整个代码是

const mongoose = require('mongoose');
const uniqueValidator = require('mongoose-unique-validator');
const Schema = mongoose.Schema;

const UserSchema = new Schema({
    name: {
        type: String,
        required: true
    },
    username: {
        type: String,
        required: true,
        unique: true,
        uniqueCaseInsensitive: true
    },
    email: {
        type: String,
        required: true
    },
    password: {
        type: String,
        required: true
    },
    date: {
        type: Date,
        default: Date.now()
    }
});

UserSchema.plugin(uniqueValidator, { message: 'Someone already has that username'});

module.exports = User = mongoose.model('users', UserSchema);

谢谢

2 个答案:

答案 0 :(得分:0)

执行return时,它将结束该函数,因此您无需执行循环的任何进一步迭代。

您需要连接每次循环迭代的值,然后在循环之后返回最终结果:

            "render": function(data, type, row) {
                //    var date1 = new Date()+10J;
                //affichage des icones  
                var x = localStorage.getItem("listeFavoris");
                var xt = x.split(',');
                var html = '';
                for (var h = 0; h < xt.length; h++) {
                  var heart = '';
                  console.log(xt[h]);
                  if (row[0] == xt[h]) {
                    heart = 'icon-heart';
                  } else {
                    heart = 'icon-heart1';
                  }
                  html += '<i class="' + heart + '" onClick="addFAv(this)"  data-id="' + row[0] + '" data-name="' + row[1] + '"></i> <a href="#" class="icon-line-clipboard i-circled i-small"></a> <a href="#" class="icon-file-alt2 i-circled i-small"></a>';
                }
              return html;
            }

答案 1 :(得分:0)

我找到了解决方法

  {"render": function(data, type, row) {
            //    var date1 = new Date()+10J;
            //affichage des icones  
            var x = localStorage.getItem("listeFavoris");
            var xt = x.split(',');
            var html = '<a href="#" class="icon-line-clipboard i-circled i-small"></a> <a href="#" class="icon-file-alt2 i-circled i-small"></a>';
            for (var h = 0; h < xt.length; h++) {
              var heart = '';
              console.log(xt[h]);
              if (row[0] == xt[h]) {
                heart = 'icon-heart';
                  h = xt.length; }
              html += '<i class="' + heart + '" onClick="addFAv(this)"  data-id="' + row[0] + '" data-name="' + row[1] + '"></i>';
            }
        if   (heart == '') {
        html += '<i class="icon-heart1" onClick="addFAv(this)"  data-id="' + row[0] + '" data-name="' + row[1] + '"></i>';}
          return html;

        }, "targets": 4}