简体   繁体   中英

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.

These elements are in an array called "AgendaItems".

There are 2 files the user can read, each is formatted with some differences. Both have 3 columns.

File 1: AgendaItem, LegistarId, Title File 2: Agenda #, File #, Title

I am able to read the file and populate the array $scope.AgendaItens with the contents of either file, depending what the user selects.

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.

Is there a way to, in the HTML choose which value to display? for example, if array has AgendaItem, display AgendaItem, if array has Agenda #, display Agenda #?

Or, is it possible to rename a the name of a key, from Agenda # to AgendaItem and from File # to 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

You can use javascript array.map extension? May be better for later reading the code.

Source data

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

Map it for cleaner usage

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; ng-if="expression"

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

...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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