1

I have an array of object :

let data = [
  { "date" : "17/03/2022", "count" : 2, "[email protected]" : 2 },
  {
    "date" : "17/05/2022",
    "count" : 2,
    "[email protected]" : 1,
    "[email protected]" : 1
  },
  { "date" : "17/07/2022", "count" : 7, "[email protected]" : 7 },
];

I would like to remove "@" in the object key instead of the email address.

This is the expected output :

// Expected output:
data = [
  { "date" : "17/03/2022", "count" : 2, "james" : 2 },
  {
    "date" : "17/05/2022",
    "count" : 2,
    "admin" : 1,
    "secretary" : 1
  },
  { "date" : "17/07/2022", "count" : 7, "staff" : 7 },
];

Notes:

I have tried, but yet not successful :

for (let i = 0; i < data.length; i++) {
  let keys = Object.keys(data[i]);
  console.log(`key-${i+1} :`, keys); // [ 'date', 'count', '[email protected]', '[email protected]' ]
  
  let emails = keys.filter(index => index.includes("@"));
  console.log(`email-${i+1} :`, emails); // [ '[email protected]', '[email protected]' ]
  
  let nameList = [];
  for (let i = 0; i < emails.length; i++) {
    let name = emails[i].split("@")[0];
    nameList.push(name);
  }
  console.log(`name-${i+1} :`, nameList); // [ 'admin', 'secretary' ]
}

Thanks in advance.

3 Answers 3

2

You could create a function which splits the keys of the object keys at @ and creates a new object using Object.fromEntries().

Here's a snippet:

const data = [{date:"17/03/2022",count:2,"[email protected]":2},{date:"17/05/2022",count:2,"[email protected]":1,"[email protected]":1},{date:"17/07/2022",count:7,"[email protected]":7}];

const converter = o => Object.fromEntries(
  Object.entries(o).map(([k, v]) => [k.split("@")[0], v])
)

console.log(
  data.map(converter)
)

If Object.fromEntries() is not supported, you could use a simple loop through the array and then each object to create new objects like this:

const output = []

for (const o of data) {
  const updated = {}
  
  for (const key in o) {
    updated[key.split("@")[0]] = o[key]
  }
  
  output.push(updated)
}
Sign up to request clarification or add additional context in comments.

1 Comment

Worked like a charm and easily understandable, much appreciated!
1

Try this as short as simple:

let data = [{
    "date": "17/03/2022",
    "count": 2,
    "[email protected]": 2
  },
  {
    "date": "17/05/2022",
    "count": 2,
    "[email protected]": 1,
    "[email protected]": 1
  },
  {
    "date": "17/07/2022",
    "count": 7,
    "[email protected]": 7
  },
];
const refinedData = JSON.parse(JSON.stringify(data));
refinedData.map((el, i) => {
  Object.keys(el).map(e => {
    if(e.includes('@')){
      refinedData[i][e.slice(0, e.indexOf('@'))] = refinedData[i][e];
      delete refinedData[i][e];
    }
  })
});

console.log(data, refinedData);

Try this improvement to prevent shallow copy and let me know your thoughts.

4 Comments

This gave the expected output, but I could not assign the data.map(...) to another variable, like : const modifiedData = data.map(...). Any advice about this? Thank you in advance.
I have improvised the code please check and let me know your thoughts
Does JSON.parse(JSON.stringify(data)) mean you are doing a deep copy to not modify the original data? That being said, one should do a deep copy before reassigning it to another variable? Thanks in advance.
Yes you are right, to prevent shallow copy of nested object.
0

Can you try using this?

for (let i = 0; i < data.length; i++) {
  let element = data[i];
  let keys = Object.keys(element);

  let emails = keys.filter(index => index.includes("@"));
  for (let j = 0; j < emails.length; j++) {
    let name = emails[j].split("@")[0];
    let value = element[emails[j]];
    Object.defineProperty(element, name, { value });
    delete element[emails[j]];
  }
}

console.log(data);

1 Comment

Thanks, but the output missed the names, it looked like this : [ { date: '17/03/2022', total: 2 }, { date: '17/05/2022', total: 2 }, { date: '17/07/2022', total: 7 } ]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.