Assigning array variables in Javascript - Stack Overflow

I am having a small issue with variable assignment. For some reason my line variable doesn't prope

I am having a small issue with variable assignment. For some reason my line variable doesn't properly assign.

var records = new Array();
var recid = -5
var subrecid = 6495;
var line = new Array();
line['recid'] = recid;
line['subrecid'] = subrecid;
if (subrecid > 0) records.push(line);

I am having a small issue with variable assignment. For some reason my line variable doesn't properly assign.

var records = new Array();
var recid = -5
var subrecid = 6495;
var line = new Array();
line['recid'] = recid;
line['subrecid'] = subrecid;
if (subrecid > 0) records.push(line);
Share Improve this question edited Dec 24, 2011 at 22:18 Ry- 226k56 gold badges493 silver badges499 bronze badges asked Dec 24, 2011 at 22:15 user1052933user1052933 1912 gold badges4 silver badges8 bronze badges 9
  • 2 Actually, it does. Instead of checking what you think is the solution, can you provide a jsFiddle showing the error? – Ry- Commented Dec 24, 2011 at 22:17
  • 2 How do you know it doesn't "assign"? What result are you expecting to get that you aren't getting? – Gareth Commented Dec 24, 2011 at 22:17
  • 1 mind you that javascript does NOT have associative arrays. You can use the 1line['syntax'] because you are actually calling an object there, but there is associative array in javascript. It might be the fact you just init it as an array? – Nanne Commented Dec 24, 2011 at 22:20
  • JavaScript doesn't need arrays for that, by the way; there's no such thing as a hash or associative array. You can use var line = {}; line.recid = recid; line.subrecid = subrecid; instead. – Ry- Commented Dec 24, 2011 at 22:22
  • 1 @MДΓΓБДLL: Yeah, it depends on your definition. – Ry- Commented Dec 24, 2011 at 22:35
 |  Show 4 more ments

3 Answers 3

Reset to default 4

Don't use an array for non-integer indexing. Use an object. Also, it's generally better to use [] instead of new Array(). Oh yeah, and there's a line missing a semicolon.

var records = [];
var recid = -5;
var subrecid = 6495;
var line = {}; // object, not array
line.recid = recid;
line.subrecid = subrecid;
if (subrecid > 0) records.push(line);

Even more concise:

var records = [];
var recid = -5;
var subrecid = 6495;
var line = {
    recid: recid,
    subrecid: subrecid
};
if (subrecid) records.push(line);

Matt's answer is fine, but you could take greater advantage of object literal syntax:

var records = [];
var line = {recid: -5, subrecid: 6495 };

if (line.subrecid > 0) records.push(line);
var val1=$('#Accountstype_account_c').val();

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745162843a4614472.html

相关推荐

  • Assigning array variables in Javascript - Stack Overflow

    I am having a small issue with variable assignment. For some reason my line variable doesn't prope

    3小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信