如何使用jquery基于其id将一个数组分成两个

Im strugling with this array:

first id = 1

items for the second id = 12,21,34,33;

second id = 2

items for the second id = 21,12,34

It looks like this in my array :

arr = [12 1,21 1,34 1,33 1,21 2,12 2,34 2]

i want to store it like this:

id | item_id

1 12

1 21

1 34

1 33

2 21

2 12

2 34

here's my code :

var item_id = new Array();

$("td.items").each(function() {

if(this){

item_id.push($(this).attr('id'));

}

});

There is a good javascript library for tables manipulations named underscore.js. You can find this here: underscore js library

jQuery is not designed for tables manipulations but for easy JS DOM operations. Make a deeper research on mensioned above underscore library. There is everything what you need.

I tried this in console

var arr = ["12 1","21 1","34 1","33 1","21 2","12 2","34 2"]
var arr1 = [];
var arr2 = []
$.each(arr,function(k,v){
  var id = v.split(" ")[1]
  var value = v.split(" ")[0]
  if (id == 1){
    arr1.push(value)
  }else if(id == 2){
    arr2.push(value)
  }
});

Result

arr1
["12", "21", "34", "33"]

arr2
["21", "12", "34"]

Do you need like this?