JavaScript dictionary with names

前端 未结 8 1190
孤街浪徒
孤街浪徒 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:29

    Another approach would be to have an array of objects, with each individual object holding the properties of a column. This slightly changes the structure of "myMappings", but makes it easy to work with:

    var myMappings = [
        { title: "Name", width: "10%" },
        { title: "Phone", width: "10%" },
        { title: "Address", width: "50%" },
        { title: "Zip", width: "10%" },
        { title: "Comments", width: "20%" }
    ];
    

    Then you could easily iterate through all your "columns" with a for loop:

    for (var i = 0; i < myMappings.length; i += 1) {
        // myMappings[i].title ...
        // myMappings[i].width ...
    }
    

提交回复
热议问题