-4

I am getting data something like this

{16: {email: 'tom@tom.tom', name: 'Tom', seq: 16}, 50: {email: 'boy@boy.boy', name: 'Boy', seq: 50}}

I want to change it to

{16: {email: '', name: 'Tom', seq: 16}, 50: {email: '', name: 'Boy', seq: 50}}

for just reset. It is not an array, so I am kinda lost. key values in the object are dynamic, so cannot just do like

const obj = {16: {email: 'tom@tom.tom', name: 'Tom', seq: 16}, 50: {email: 'boy@boy.boy', name: 'Boy', seq: 50}}
obj['16'] = {email: '', name: 'Tom', seq: 16}

and also it might have more than two objects, so also cannot just hard coding like

const obj = {16: {email: 'tom@tom.tom', name: 'Tom', seq: 16}, 50: {email: 'boy@boy.boy', name: 'Boy', seq: 50}}
obj['16'] = {email: '', name: 'Tom', seq: 16}
obj['50'] = {email: '', name: 'Tom', seq: 50}

could anyone help me? :(

DarkBee
  • 16,592
  • 6
  • 46
  • 58

2 Answers2

0

If you want to convert and manipulate your data as an array, I suggest that you use Object.entries() and map(). Then you can reset the email values through forEach.

let obj = {
  16: { email: 'tom@tom.tom', name: 'Tom', seq: 16 },
  50: { email: 'boy@boy.boy', name: 'Boy', seq: 50 },
};

let newObj = Object.entries(obj).map(([el, obj]) => ({ ...obj }));

newObj.forEach((el, index) => {
  el.email = '';
});

console.log(newObj);
mplungjan
  • 169,008
  • 28
  • 173
  • 236
-1

let data = {
  16: {email: 'tom@tom.tom', name: 'Tom', seq: 16},
  50: {email: 'boy@boy.boy', name: 'Boy', seq: 50}
};

for (let key in data) {
  if (data.hasOwnProperty(key)) {
    data[key].email = '';
  }
}

console.log(data);
mplungjan
  • 169,008
  • 28
  • 173
  • 236
xuān
  • 11
  • 1