简体   繁体   中英

Renaming object properties in an array of objects

I have the following structure:

const data = {
    invoices: [
        {
            Date: "2018-12-18T00:00:00.000Z",
            InvoiceNumber: "59"
        },
        {
            Date: "2018-12-18T00:00:00.000Z",
            InvoiceNumber: "59"
        }
    ]
};

I am wanting to re-name all instances of InvoiceNumber to CreditNoteNumber to give me the following:

const data = {
    invoices: [
        {
            Date: "2018-12-18T00:00:00.000Z",
            CreditNoteNumber: "59"
        },
        {
            Date: "2018-12-18T00:00:00.000Z",
            CreditNoteNumber: "59"
        }
    ]
};

I have tried various things like the following for example:

var changed = data.invoices.map(function(item) {
    return {
        ...data.invoices,
        CreditNoteNumber: item.InvoiceNumber
    };
});

However the spread pushes CreditNoteNumber outside the object.

Just use destructure and rename

 const data = { invoices: [ { Date: "2018-12-18T00:00:00.000Z", InvoiceNumber: "59" }, { Date: "2018-12-18T00:00:00.000Z", InvoiceNumber: "59" } ] }; var changed = data.invoices.map( ({ Date, InvoiceNumber: CreditNoteNumber }) => ({ Date, CreditNoteNumber }) ); console.log(changed);

There is no such operation as "renaming" a property in JavaScript.

You will need to delete the property you no longer wish to keep:

 const data = { invoices: [ { Date: "2018-12-18T00:00:00.000Z", InvoiceNumber: "59" }, { Date: "2018-12-18T00:00:00.000Z", InvoiceNumber: "59" } ] }; const changed = data.invoices.map(item => { const obj = { ...item, CreditNoteNumber: item.InvoiceNumber }; delete obj.InvoiceNumber return obj }); console.log(changed)

var changed = data.invoices.map(function(item) {
  return {
    Date: item.Date,
    CreditNoteNumber: item.InvoiceNumber
  };
});

You can use the function map along with the function Object.assign as follow:

The following approach doesn't mutate the original object

 const data = {invoices: [{Date: "2018-12-18T00:00:00.000Z",InvoiceNumber: "59"},{Date: "2018-12-18T00:00:00.000Z",InvoiceNumber: "59"}]}, result = data.invoices.map(({InvoiceNumber: CreditNoteNumber, ...items}) => Object.assign({}, items, {CreditNoteNumber})); console.log(result);

If you want to mutate the original object, you can use a forEach instead:

 const data = {invoices: [{Date: "2018-12-18T00:00:00.000Z",InvoiceNumber: "59"},{Date: "2018-12-18T00:00:00.000Z",InvoiceNumber: "59"}]}; data.invoices.forEach((item) => { item.CreditNoteNumber = item.InvoiceNumber; delete item.InvoiceNumber; }); console.log(data);

the best option is to create a new object with the property item.CreditNoteNumber and use a for each loop to iterate and reassign the values to the new object.

once the new object is created, assign the new object to the original object data.invoices.

const data = {invoices: [{Date: "2018-12-18T00:00:00.000Z",InvoiceNumber: "59"},{Date: "2018-12-18T00:00:00.000Z",InvoiceNumber: "59"}]};

var newInvoices = [];

data.invoices.forEach((item) => {
 let invoice = {};
     invoice.Date = item.Date;
     invoice.CreditNoteNumber = item.InvoiceNumber;

  newInvoice.push(invoice);
});

data.invoices = newInvoices;

console.log(data.invoices);

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