2014-08-29 83 views
0

想想看,我有如下一个对象数组:更改对象数组的顺序

var array = [ 
    { name: "A", position: "0" }, 
    { name: "B", position: "1" }, 
    { name: "C", position: "2" }, 
    { name: "D", position: "3" }, 
    { name: "E", position: "4" }, 
    { name: "F", position: "5" } 
]; 

比方说,用户拖动有4位的元素,把它有位置1

var replacedItem = { name: "E", position: "4" }; 
var destinationItem = { name: "B", position: "1" }; 
的元素

如何可以重新排列使用javascript具有包含下列值的阵列元素的位置:

var array = [ 
    { name: "A", position: "0" }, 
    { name: "E", position: "1" }, 
    { name: "B", position: "2" }, 
    { name: "C", position: "3" }, 
    { name: "D", position: "4" }, 
    { name: "F", position: "5" } 
]; 

个谢谢,

+0

不应该B获得位置4? – 2014-08-29 08:09:52

+0

你应该看看array.splice,这可以重新排列 – AirBorne04 2014-08-29 08:11:49

回答

1

1 - destionNationItemIndex

4 - replacedItemIndex

你可以试试这个: -

var removed = array.splice(4, 1); 
array.splice(1, 0, removed[0]); 
for (var i = 1; i <= 4; i++) { 
    array[i].position = (parseInt(array[i - 1].position) + 1).toString(); 
} 

打印你所需要的: -

var array = [ 
    { name: "A", position: "0" }, 
    { name: "E", position: "1" }, 
    { name: "B", position: "2" }, 
    { name: "C", position: "3" }, 
    { name: "D", position: "4" }, 
    { name: "F", position: "5" } 
] 
+0

在'parseInt'中使用radix参数是一种很好的做法 – thefourtheye 2014-09-01 09:36:31

1

可以使用splice分两步移动元素:

var a = [ ... ]; 
var temp = a.splice(4, 1); // remove element at index 4 
a.splice(1, 0, temp[0]); // insert the removed element at index 1 

splice()返回被删除的元素的数组(在这种情况下,1个元素的数组),这就是为什么你需要下标temp

既然你只需要左右移动name属性,一个方法是将数组分成两个阵列,重新排列对应name属性阵列和重组:

var fromIndex = 4, toIndex = 1; 
// Step 1: collect names and positions in separate arrays 
var names = [], positions = []; 
array.forEach(function(elt) { 
     this.names.push(elt.name); 
     this.positions.push(elt.position); 
    }, {names: names, positions: positions}); 
// Step 2: do a circular permutation of the names between the indexes (inclusive) 
var temp = names.splice(fromIndex, 1); 
names.splice(toIndex, 0, temp[0]); 
// Step 3: recombine names and positions into an array of objects 
var n = array.length; 
array.length = 0; 
for (var i = 0; i < n; ++i) { 
    array.push({name: names[i], position: positions[i]}); 
} 
+0

但是这不会改变对象的位置属性的值! – 2014-08-29 08:14:29

+0

@AbdulJabbar - 哎呀。误解了问题。会考虑解决这个问题。 – 2014-08-29 08:15:06

+0

@AbdulJabbar - 现在修复了。 – 2014-08-29 08:28:15

-2

这是最简单的逻辑实现的是:

var array = [ 
    { name: "A", position: "0" }, 
    { name: "B", position: "1" }, 
    { name: "C", position: "2" }, 
    { name: "D", position: "3" }, 
    { name: "E", position: "4" }, 
    { name: "F", position: "5" } 
]; 

var replacedItem = { name: "E", position: "4" }; 
var destinationItem = { name: "B", position: "1" }; 

for(var i=0; i<array.length; i++){ 
    if(array[i].position == replacedItem.position) 
     array[i].position = destinationItem.position; 
    else if(array[i].position == destinationItem.position) 
     array[i].position = replacedItem.position; 
} 

新阵列将是:

var array = [ 
     { name: "A", position: "0" }, 
     { name: "B", position: "4" }, 
     { name: "C", position: "2" }, 
     { name: "D", position: "3" }, 
     { name: "E", position: "1" }, 
     { name: "F", position: "5" } 
    ]; 

你只需要交换他们的位置。

DEMO

+0

这个例子做了一个简单的交换操作,我想我们不需要任何迭代就可以做到这一点。如果你仔细检查我的问题的最终状态,你会发现它与你的结果有所不同。感谢您的回答。 – anilca 2014-08-29 08:34:01

+1

即使交换位置后,您的结果也不符合OP的需求。 – 2014-08-29 08:34:04