2011-10-18 51 views
0

我有一个数字javascript数组,其中包含几个包含地理数据的对象。 我需要做的是在该数组中的特定对象之后添加新对象的动态计数。向javascript数组添加新内容

我知道,有拼接功能,但我不知道,如何使新对象的计数变量。

myArray.splice(pos, 0, ...); 

我得到了什么问题?

回答

1

希望我明白你的意思。

var 
    oldA = [1, 2, 3], 
    newA = [4, 5]; 

oldA.splice.apply(oldA, (function (index, howMany, elements) { 
    // this is actually building the arguments array (2nd parameter) 
    // for the oldA.splice call 
    elements = elements.slice(); 
    elements.splice(0, 0, index, howMany); 

    return elements; 
}(/*index to insert at*/ 2, /*howMany to remove*/ 0, /*elements to insert*/ newA))); 

console.log(oldA, newA); // [1, 2, 4, 5, 3] [4, 5] 
+0

谢谢,我认为你做到了!我只是试一试并回报。 – madc

+0

工程就像一个魅力,谢谢! – madc