javascript 如何在javascript中创建列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5711369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 18:05:13  来源:igfitidea点击:

How to create a list in javascript

javascriptasp.net

提问by Ulhas Tuscano

Javascript can read the list using for loop. e.g

Javascript 可以使用 for 循环读取列表。例如

 [WebMethod]
    public static List<EmpName> GetData(int startIndex, int maximumRows, string sort, string filter)
    {
        var emp = objClient.GetData(startIndex, maximumRows, sort, filter);
        List<EmpName> lstEmp = new List<EmpName>();
        foreach (var item in emp)
        {
            EmpName objEmp = new EmpName();
            objEmp.ID = item.ID;
            objEmp.Name = item.Name;
            lstEmp.Add(objEmp);
        }
        return lstEmp;
    }

Javascript:
function ReadList(lstEmp)
{
     for(var i=0;i<lstEmp.length;i++)
     {
          alert(lstEmp[i].ID+" "+ lstEmp[i].Name);
     }
}

I want to create a list in javascript i.e List to perform various operation at client side how it can be achieved?

我想在javascript ie List 中创建一个列表来在客户端执行各种操作它是如何实现的?

回答by Archan Mishra

There are multiple ways to create a List in JS.

在 JS 中有多种创建 List 的方法。

The easiest one being

最简单的一种是

var l = [];
l[0] = "a";
l[1] = 1;

another way todo so is

另一种方法是

var l= [1,"as",func];

refer W3Schools

参考W3Schools