JavaScript dictionary with names

前端 未结 8 1175
孤街浪徒
孤街浪徒 2020-12-12 18:08

I want to create a dictionary in JavaScript like the following:

myMappings = [
    { "Name": 10%},
    { "Phone": 10%},
    { "Addres         


        
8条回答
  •  温柔的废话
    2020-12-12 18:38

    The main problem I see with what you have is that it's difficult to loop through, for populating a table.

    Simply use an array of arrays:

    var myMappings = [
        ["Name", "10%"], // Note the quotes around "10%"
        ["Phone", "10%"],
        // etc..
    ];
    

    ... which simplifies access:

    myMappings[0][0]; // column name
    myMappings[0][1]; // column width
    

    Alternatively:

    var myMappings = {
        names: ["Name", "Phone", etc...],
        widths: ["10%", "10%", etc...]
    };
    

    And access with:

    myMappings.names[0];
    myMappings.widths[0];
    

提交回复
热议问题