skip to Main Content

I am getting data something like this

{16: {email: '[email protected]', name: 'Tom', seq: 16}, 50: {email: '[email protected]', 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: '[email protected]', name: 'Tom', seq: 16}, 50: {email: '[email protected]', 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: '[email protected]', name: 'Tom', seq: 16}, 50: {email: '[email protected]', name: 'Boy', seq: 50}}
obj['16'] = {email: '', name: 'Tom', seq: 16}
obj['50'] = {email: '', name: 'Tom', seq: 50}

could anyone help me? 🙁

2

Answers


  1. let data = {
      16: {email: '[email protected]', name: 'Tom', seq: 16},
      50: {email: '[email protected]', name: 'Boy', seq: 50}
    };
    
    for (let key in data) {
      if (data.hasOwnProperty(key)) {
        data[key].email = '';
      }
    }
    
    console.log(data);
    
    Login or Signup to reply.
  2. let obj = {
      16: { email: '[email protected]', name: 'Tom', seq: 16 },
      50: { email: '[email protected]', name: 'Boy', seq: 50 },
    };
    
    let newObj = Object.entries(obj).map(([el, obj]) => ({ ...obj }));
    
    newObj.forEach((el, index) => {
      el.email = '';
    });
    
    console.log(newObj);

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search