简体   繁体   English

JavaScript按顺序排列数组

[英]JavaScript keeping array in order

I am trying to keep an array in a particular order, and I've learned about the splice function to insert in a specific index location, but it doesn't seem to do the right thing. 我试图以特定顺序保留数组,并且我了解了要在特定索引位置插入的拼接函数,但是这样做似乎不正确。

Here's a fiddle of what I have so far: http://jsfiddle.net/drumichael/7r2pV/277/ 这是到目前为止我所掌握的东西: http : //jsfiddle.net/drumichael/7r2pV/277/

In section 1, you can select one or many of the options, however "First" should always be 1st, "second" 2nd, and "Fourth" after both of those. 在第1节中,您可以选择一个或多个选项,但是“ First”应始终是第一个,“ second”,第二个和“ Fourth”。 If you only choose "First" & "Fourth", they should be in that order regardless of the order in which you checked the boxes. 如果您仅选择“第一”和“第四”,则无论您选中框的顺序如何,它们都应采用该顺序。

In section 2 - you may only choose ONE of Options A, B, & C... and that option should always be in the 3rd position of the array (unless you didn't choose ANY items from Section 1 of course, it would be all by itself inside the array). 在第2节中,您只能选择选项A,B和C中的一个,并且该选项应始终位于数组的第3个位置(除非您没有从第1节中选择任何项目,否则它将本身全部放在数组中)。

Honestly I'm not sure where to go from here. 老实说,我不确定从这里出发。 I've tried this: 我已经试过了:

var array_position = $(this).attr('id');
var messagePart = $(this).attr('data-message');

message.splice(array_position, 0, messagePart);

But they still dont end up in the proper order. 但是他们仍然没有以正确的顺序结束。

When using splice() the index the item is inserted to should be 0 -indexed. 使用splice() ,要插入项目的索引应该为0 -indexed。 For example: 例如:

var arr = [1,2,3,5,6];
arr.splice(3, 0, 4);

will insert 4 into the 3rd 0 -indexed location. 会将4插入第3个0索引位置。

Also, you need to use 0 as the 2nd parameter in splice when inserting a value like you want. 另外,在插入所需的值时,需要在splice使用0作为第二个参数。

To make your code work you should change the id parameter of each input to be 0 -indexed, and then use splice() to insert the item. 为了使代码正常工作,您应该将每个输入的id参数更改为0索引,然后使用splice()插入该项。

$(".mycheckbox").on("change", function() {
  var message = [];
  $('.mycheckbox:checked').each(function() {
    var messagePart = $(this).attr('data-message');
    message.splice($(this).attr('id'), 0,messagePart);
  });
  $(".summary").html(message.join(", "));
});

Here's a working example: 这是一个工作示例:

 $(".mycheckbox").on("change", function() { var message = []; $('.mycheckbox:checked').each(function() { var messagePart = $(this).attr('data-message'); message.splice($(this).attr('id'), 0,messagePart); }); $(".summary").html(message.join(", ")); }); $("input:checkbox").on('click', function() { // in the handler, 'this' refers to the box clicked on var $box = $(this); if ($box.is(":checked")) { // the name of the box is retrieved using the .attr() method // as it is assumed and expected to be immutable var group = "input:checkbox[name='" + $box.attr("name") + "']"; // the checked state of the group/box on the other hand will change // and the current value is retrieved using .prop() method $(group).prop("checked", false); $box.prop("checked", true); } else { $box.prop("checked", false); } }); 
 .selection_area { border: 1px solid gray; margin-bottom: 10px; padding: 10px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="selection_area"> <h4>Select as many of these options as desired</h4> <p>Should appear in numerical order</p> <input type="checkbox" class="mycheckbox" id="1" data-message="Second" />Second <br/> <input type="checkbox" class="mycheckbox" id="0" data-message="First" />First <br/> <input type="checkbox" class="mycheckbox" id="3" data-message="Fourth" />Fourth <br/> </div> <h5>Summary of Selections:</h5> <div class="summary"></div> 

Here's an updated JSFiddle . 这是更新的JSFiddle

Since the id's are numbered in the order you want, you can sort $('.mycheckbox:checked') by id. 由于ID是按所需顺序编号的,因此您可以按ID对$('.mycheckbox:checked')进行排序。 Then use the $.each function. 然后使用$.each函数。

$(".mycheckbox").on("change", function() {
  var message = [];
  $('.mycheckbox:checked').sort(function(a, b) {
    return parseInt(a.id) > parseInt(b.id);
  }).each(function() {
    var messagePart = $(this).attr('data-message');
    message.push(messagePart);
  });
  $(".summary").html(message.join(", "));
});

DEMO: http://jsfiddle.net/kom7hd5o/ 演示: http : //jsfiddle.net/kom7hd5o/

Resource: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort 资源: https //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM