在数组中添加数组对象或在javascript中在数组中添加新索引

时间:2015-05-13 10:08:29

标签: javascript arrays multidimensional-array

我在javascript文件中有一个对象

The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2015-05-13 14:39:54.597 Traquer[5594:207956] {
            "message": { "email": "email is required " , "fname": "fname is required " , "lname": "lname is required " , "password": "password is required " , "birthDate": "birthDate is required " , "zip": "zip is required "  },
            "boolean": false,
            "eType": 0
    }

我需要在arr1中添加新对象。它应该是这样的..

myArray:{
  id:'',
  arr1:[],
  arr2:[]
}

是的,这应该是结构。香港专业教育学院曾尝试使用array.push,但无法正常工作。

请帮忙。谢谢!

4 个答案:

答案 0 :(得分:1)

因为Vigneswaran提到它的对象不是数组 你可以直接那样做

myArray.arr1={index1:'',index2:''};

答案 1 :(得分:0)

myArray不是数组,它是一个Object。 如果我理解正确,你可以得到你想要的东西..

var myArray = {
              id:'',
              arr1:{},
              arr2:{}
            };  
console.log(myArray);           
myArray.arr1 = { index1:'', index2:''};
myArray.arr2 = { index1:'', index2:''};
console.log(myArray);

答案 2 :(得分:0)

我同意对此的评论。 /* I wont print "*/" I will print as the "*/" is seen as the end of the comment This line prints */ ## This line will also print at x has not been set back to one 是一个对象。要访问媒体资源,您必须使用myArraymyArray.propertyarr1arr2的属性,因此请使用myArray

答案 3 :(得分:0)

var myArray = {
  id:'',
  arr1:[],
  arr2:[]
};
alert(JSON.stringify(myArray));
delete myArray['arr1'];
delete myArray['arr2'];
myArray['arr1'] ={
   index1:'',
   index2:''
  };
myArray['arr2'] = {
   index1:'',
   index2:''
  };
alert(JSON.stringify(myArray));  

<强> DEMO