简体   繁体   English

AngularJS/JavaScript - 如何重命名数组键名

[英]AngularJS/JavaScript - How to rename an array key name

I have an application that reads an excel file, and displays its contents in an html table.我有一个应用程序读取 excel 文件,并在 html 表中显示其内容。

These elements are in an array called "AgendaItems".这些元素位于名为“AgendaItems”的数组中。

There are 2 files the user can read, each is formatted with some differences.用户可以阅读 2 个文件,每个文件的格式都有一些差异。 Both have 3 columns.两者都有 3 列。

File 1: AgendaItem, LegistarId, Title File 2: Agenda #, File #, Title文件 1:AgendaItem、LegistarId、标题 文件 2:议程#、文件#、标题

I am able to read the file and populate the array $scope.AgendaItens with the contents of either file, depending what the user selects.我能够读取文件并使用任一文件的内容填充数组 $scope.AgendaItens,具体取决于用户的选择。

My problem is that before making these modifications to accept a second file with a different format, I used:我的问题是,在进行这些修改以接受具有不同格式的第二个文件之前,我使用了:

<td scope="row">{{ai.AgendaItem}}</td>
<td>{{ai.LegistarID}}</td>

When processing the new file, now the array contains: Agenda #, and File #, which are equivalent to AgendaItem, and LegistarID respectively.在处理新文件时,现在数组中包含:Agenda#、File#,分别相当于AgendaItem、LegistarID。

Is there a way to, in the HTML choose which value to display?有没有办法,在HTML中选择显示哪个值? for example, if array has AgendaItem, display AgendaItem, if array has Agenda #, display Agenda #?例如,如果数组有 AgendaItem,则显示 AgendaItem,如果数组有 Agenda #,则显示 Agenda #?

Or, is it possible to rename a the name of a key, from Agenda # to AgendaItem and from File # to LegistarID?或者,是否可以重命名密钥的名称,从 Agenda # 到 AgendaItem,从 File # 到 LegistarID?

Please let me know if I need to post more details, more code in order for me to be able to get help.如果我需要发布更多详细信息,更多代码以便我能够获得帮助,请告诉我。

Thank you in advance, Erasmo提前谢谢你, Erasmo

You can use javascript array.map extension?您可以使用 javascript array.map 扩展名吗? May be better for later reading the code.以后阅读代码可能会更好。

Source data源数据

var source = [{"Agenda #":"1","File #":"file1.xls"}]

Map it for cleaner usage Map 用于清洁用途

var maped = source.map(function(p) {
    return {"AgendaItem":p["Agenda #"], "LegistarID":p["File #"]}
})

Other wise you have to use Object[fieldName] usage in html.(not suggesting to do so) like;否则,您必须在 html 中使用 Object[fieldName] 用法(不建议这样做); ng-if="expression" ng-if="表达式"

  < ... ng-if='data["Agenda #"]' > {{data["Agenda #"]}}

... ...

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

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