简体   繁体   English

JavaScript多维数组问题

[英]Javascript multi dimensional array issue

I want to create an array like this: 我想创建一个像这样的数组:

send[1]['conversation_group_id']=1;

But i get an error that cannot set property conversation_group_id of undefined. 但是我收到一个错误,无法设置未定义的属性session_group_id。 What am i doing wrong? 我究竟做错了什么?

Thanks! 谢谢!

Even though you have initialized send with [], which makes it an array, send[1] will be undefined, you will need to initialize it as an object too before you can set a property inside it. 即使您已经使用[]初始化了send [],这使它成为一个数组,但send [1]将是未定义的,但您还需要在将其设置为属性之前将其初始化为对象。 At the moment you are trying to set a property of undefined. 目前,您正在尝试设置未定义的属性。

 var send = []; console.log(send[1]); send[1] = {}; send[1]['conversation_group_id']=1; console.log(send[1]['conversation_group_id']); 

Your data structure is an array of objects. 您的数据结构是一个对象数组。 To initialize it, you can also do it this way. 要初始化它,您也可以通过这种方式进行。

send = [null, {conversation_group_id: 1}]

Nothing wrong with @Dij's answer , but just though it's worth mentioning an alternative on how to initialize the structure you're looking for. @Dij的答案没有错,但是尽管值得一提的是如何初始化您要寻找的结构的替代方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM