如何用数组键替换值id jquery,在spice之后将值id更改为数组键

时间:2018-01-16 06:20:07

标签: javascript jquery arrays

我在使用数组时遇到了一些问题,如何在拼接后使用键(new_index)更改 id ?在jquery成功拼接,但如何在数组中改变值" id" ..



var each_arr = [{
    id: 0,
    judul: "JUDUL 1"
},
{
    id: 1,
    judul: "ICIK ICIK ehem"
},
{
    id: 2,
    judul: "ASOLOLE"
},
{
    id: 3,
    judul: "IWAK PEYEK"
}
];

$(".radio1").on('change', function(event, state) {
    var idarr = $(this).data("id");
    each_arr.move(idarr, 0);
    console.log(each_arr);
});

Array.prototype.move = function(old_index, new_index) {
    if (new_index >= this.length) {
        var k = new_index - this.length;
        while ((k--) + 1) {
            this.push(undefined);
        }
    }
    this.splice(new_index, 0, this.splice(old_index, 1)[0]);
    return this;
};

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="radio" name="radio1" class="radio1" data-id="0">0
<input type="radio" name="radio1" class="radio1" data-id="1">1
<input type="radio" name="radio1" class="radio1" data-id="2">2
<input type="radio" name="radio1" class="radio1" data-id="3">3
&#13;
&#13;
&#13;

脚本正在运行,但如何更改值&#34; id&#34;拼接后用数组键??

2 个答案:

答案 0 :(得分:0)

您可以使用此功能代替 selectedQuestions:string[] = []; clickSelectBox(itemKey){ console.log(itemKey); const foundAt = this.selectedQuestions.indexOf(itemKey); console.log(foundAt); if (foundAt >= 0) { this.selectedQuestions.splice(foundAt, 1); } else { this.selectedQuestions.push(itemKey); } console.log(this.selectedQuestions); }

move()

修改

function move(array, value, positionChange) {
  var oldIndex = array.indexOf(value);
  if (oldIndex > -1){
    var newIndex = (oldIndex + positionChange);

    if (newIndex < 0){
      newIndex = 0
    }else if (newIndex >= array.length){
      newIndex = array.length
    }

    var arrayClone = array.slice();
    arrayClone.splice(oldIndex,1);
    arrayClone.splice(newIndex,0,value);

    return arrayClone
  }
  return array
}

答案 1 :(得分:0)

试试这个,我没有改变你的排序逻辑,只是添加了一个map函数来更新id和arr key一样

this.map((arr, i) => ({...arr, id:i}));

&#13;
&#13;
var each_arr = [{
    id: 0,
    judul: "JUDUL 1"
  },
  {
    id: 1,
    judul: "ICIK ICIK ehem"
  },
  {
    id: 2,
    judul: "ASOLOLE"
  },
  {
    id: 3,
    judul: "IWAK PEYEK"
  }
];

$(".radio1").on('change', function(event, state) {
  var idarr = $(this).data("id");
  each_arr = each_arr.move(idarr, 0);
  console.log(each_arr);
});

Array.prototype.move = function(old_index, new_index) {
  if (new_index >= this.length) {
    var k = new_index - this.length;
    while ((k--) + 1) {
      this.push(undefined);
    }
  }
  this.splice(new_index, 0, this.splice(old_index, 1)[0]);
  return this.map((arr, i) => ({...arr, id:i}));
};
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="radio" name="radio1" class="radio1" data-id="0">0
<input type="radio" name="radio1" class="radio1" data-id="1">1
<input type="radio" name="radio1" class="radio1" data-id="2">2
<input type="radio" name="radio1" class="radio1" data-id="3">3
&#13;
&#13;
&#13;