Home > OS >  JavsScript/AngularJS: How to replace/remove part of a property name
JavsScript/AngularJS: How to replace/remove part of a property name

Time:01-27

I have this piece of code that removes characters such as whitespace from property names:

            let result = rows.map(el => {
                let resultobj = {};
                Object.entries(el).map(([key, val]) => {      
                  resultobj[key.split(" ").join("").replace(/\W /g, '')] = val;
                  return resultobj;
            } )
              return resultobj;
            })

There is a property named "AgendaItem", I would like to remove the word "Item" from the name, leaving only "Agenda". How can I add this requirement to the current code, above?

Thank you for your help, Erasmo

UPDATE - I tored the code below, to replace File # with Legistar, and LegistarID with Legistar

        let result = rows.map(el => {
                
                let resultobj = {};
                
                Object.entries(el).map(([key, val]) => {                        

                    resultobj[key.split(" ").join("").replace(/\W |Item$/g, '').replace("File #","Legistar").replace("LegistarID","Legistar")] = val;                       
                
                    return resultobj;
                })
                return resultobj;
            })

            console.log(result);

After execution, result contains:

0
: 
{File: '75588', Ver: '1', Agenda: '1', BoardsCommissionsandCommittees: 'Public Comment', Type: 'Public Comment', …}
1
: 
{File: '75590', Ver: '1', Agenda: '2', BoardsCommissionsandCommittees: 'Lake Update', Type: 'Miscellaneous', …}
2
: 
{File: '75592', Ver: '1', Agenda: '3', BoardsCommissionsandCommittees: 'Booking Pace Update:', Type: 'Miscellaneous', …}
3
: 
{File: '75594', Ver: '1', Agenda: '4', BoardsCommissionsandCommittees: 'Finance Report: ', Type: 'Miscellaneous', …}
4
: 
{File: '75595', Ver: '1', Agenda: '5', BoardsCommissionsandCommittees: 'Director’s Report: ', Type: 'Miscellaneous', …}
5
: 
{File: '75596', Ver: '1', Agenda: '6', BoardsCommissionsandCommittees: 'Announcement from the Chair: , Chair', Type: 'Miscellaneous', …}

CodePudding user response:

You can extend the regex with other stuff you want deleted. Also, that split(" ").join("") is not needed as your current regex already matches spaces.

Finally, you could use Object.fromEntries to build the object in a functional way:

let rows = [{ AgendaItem: 1, "test this": 2, "one, two, three": 3 }]; 
let result = rows.map(el => Object.fromEntries(
    Object.entries(el).map(([key, val]) => [key.replace(/\W |Item$/g, ''), val])
));

console.log(result);

The $ in the regex makes sure Item is only removed when it is the last part of the key. If you want to remove it from anywhere in the key, then drop that $.

Alternatively, you could define a translation object where the key is the from-text and the value the target text:

const translation = {
    "AgendaItem": "Agenda",
    "File #": "Legistar",
    "LegistarID": "Legistar"
};

const objectMapper = obj => Object.fromEntries(
    Object.entries(obj).map(([key, val]) => [
        translation[key] ?? key.replace(/\W /g, ''), 
        val
    ])
);

// Demo
const rows = [
    { AgendaItem: 1, "test this": 2, "one, two, three": 3 },
    { LegistarID: 9, "///abc///": 20 },
    { "File #": 12 }
]; 
const result = rows.map(objectMapper);

console.log(result);

CodePudding user response:

You can check if the key contains the word "Item", if it does, you can replace the "Item" with an empty string. This will remove the word "Item" from any property that have it (not only "AgendaItem"). The rest of the code should work fine as you are already overwriting the properties.

if (key.includes("Item")) {
  key = key.replace("Item", "");
}

The if statement can also be changed to key.endsWith("Item"), it would work for your "AgendaItem" scenario.

I think that should do it.

  • Related