Javascript推送到全局对象

时间:2016-06-15 06:46:46

标签: javascript javascript-objects

我需要将元素推送到全局目标,我确切地说我需要'推'而不是'添加':

var Obj = {};
var myint  = [10,20,30];
Obj.myint = car;
// now i need to push again to car
var myint_two = [80,78,43]
Obj.myint.push(myint_two) // error because objects don't support push.
Obj.myint = myint_two // will override the previous values.
Obj.myint.concat(myint_two) // not working

关于你,我怎样才能将值推送到现有的对象元素?

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

问题是在尝试推送第二个数组之前,您没有声明Obj.myint

使用此代码可以正常工作:

var myint  = [10,20,30]
var Obj = {};

// here i set Obj.myint
Obj.myint= myInt
var myint_two = [80,78,43]
Obj.myint.concat(myint_two)

//结果 [10,20,30,80,78,43]