Javascript:用函数调用数组

时间:2014-03-11 10:48:36

标签: javascript arrays function

我对讲师的分配感到有些困惑,他让我用函数调用数组:

a[0]["Company_name"] = "Microsoft";
a[0]["employee_total"] = 20;

使用功能:

function find_employeeTotal(a, Company_name){
}

输出:

Mirosoft
20

请帮助我,谢谢

1 个答案:

答案 0 :(得分:1)

a[0]["Company_name"] = "Microsoft";
a[0]["employee_total"] = 20;
a[1]["Company_name"] = "Apple";
a[1]["employee_total"] = 10;
Company_name = "Microsoft";
Total_Employees = find_employeeTotal(a, Company_name);

    function find_employeeTotal(a, Company_name){
       for (var i = 0; i < a.length; ++i){
          if(a[i]["Company_name"] == Company_name){
             return a[i]["employee_total"];
          }
       }
       return "Company doesn't exist in array!";
    }

由于您有二维数组,我认为您要查询第一个维度以查找正确的公司,然后查询第二个维度以查找emplyoees的总数。这应该做到。

如果我弄错了,请告诉我。