简体   繁体   English

Javascript 中的二维数组

[英]2D array in Javascript

I'm trying to create two-column array and insert few rows.我正在尝试创建两列数组并插入几行。 I noticed that third row is not getting through due to error in my code.我注意到由于我的代码错误,第三行没有通过。 Currently it seems to has wrongly defined structure where one pair is assigned to first row, second pair goes to second column, third is skipped.目前它似乎有错误定义的结构,其中一对分配给第一行,第二对进入第二列,第三对被跳过。

And my goals is to have NAME|Salary table.我的目标是拥有 NAME|Salary 表。

//2D array
var e = [];
var numberofemployees = 0;

function addEmployees(name, salary){
    var arMod = e.push([name,salary]);
    return e;
    //return  Object.keys(arMod).length;
}

addEmployees(["Mark", 5000], ["Jack", 1500], ["Maria", 2000]);

Your function is designed to take two arguments, not three pairs.您的 function 设计为采用两个 arguments,而不是三对。 So call it like:所以这样称呼它:

addEmployees("Mark", 5000);
addEmployees("Jack", 1500);
addEmployees("Maria", 2000);

If on the other hand you want to be able to call the function like you did, then you need to change the function so it can deal with that format:另一方面,如果您希望能够像以前那样调用 function,那么您需要更改 function 以便它可以处理该格式:

function addEmployees(...pairs){
    return e.concat(pairs);
}
addEmployees(["Mark", 5000], ["Jack", 1500], ["Maria", 2000]);

The code you have in comments seems to indicate you expect that arMod is some object, but it is a number: push returns the length of the array.您在评论中的代码似乎表明您期望arMod是一些 object,但它是一个数字: push返回数组的长度。

Your method only accepts two arguments, so you are adding those first two arrays you are passing to your method to the array.您的方法只接受两个 arguments,因此您将传递给方法的前两个 arrays 添加到数组中。

You would need to call the addEmployees() Method for each array(name/salary pair) individually您需要分别为每个数组(姓名/薪水对)调用 addEmployees() 方法

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

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