0

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', …}
erasmo carlos
  • 664
  • 5
  • 16
  • 37
  • See [JavaScript: Object Rename Key](https://stackoverflow.com/questions/4647817/javascript-object-rename-key). – jarmod Jan 26 '23 at 21:18
  • I made an update, does that look correct? – erasmo carlos Jan 26 '23 at 21:51
  • Reverse the assignment: `resultobj['Agenda'] = resultobj['AgendaItem']`. But, you would do this *after* completely populating resultobj, not for each item in the loop. That said, you could simply use something like `resultobj[key === 'AgendaItem' ? 'Agenda' : key] = value` given that you're not trying to modify the key in the original source object. – jarmod Jan 26 '23 at 22:01

2 Answers2

1

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);
trincot
  • 317,000
  • 35
  • 244
  • 286
0

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.