简体   繁体   English

将对象推入数组

[英]Pushing Object into an Array

My understanding of JS objects is weak.我对JS对象的理解很弱。 If I pass a set of objects into a new array, then try and get the length or value at a certain index (as extract below) I return an undefined message.如果我将一组对象传递到一个新数组中,然后尝试获取某个索引处的长度或值(如下文摘录),我将返回一条未定义的消息。 Is there an obvious mistake here?这里有明显的错误吗? Thanks.谢谢。

var arrayTest = [];
var objCollection = obj.name + obj.size + obj.weight; 
arrayTest.push(objCollection);
alert(arrayTest[0]); // returns undefined

To clarify, I'm collecting a customer's product selection and returning its technical data stored in a json file - with the intention of placing this selection into a table.澄清一下,我正在收集客户的产品选择并返回其存储在 json 文件中的技术数据 - 目的是将此选择放入表格中。

{
"name": "the name",
"size": "the size",
"weight": "the weight"
},

Are you sure objCollection actually contains a value?您确定objCollection实际上包含一个值吗? It looks to me like one of the fields of the obj object is undefined as concatenating a value with an undefined object yields undefined. 在我看来, obj对象的字段之一是未定义的,因为将值与未定义的对象连接会产生未定义。 And thus, you push an undefined object to your array instead of what you want. 因此,您将一个未定义的对象而不是您想要的对象推送到您的数组中。 See Bergi's comment for correction.请参阅Bergi 的评论以进行更正。

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

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