2

how to change type array object to format object without looping method in vue.js?

example:

// Data Array
data = [
    {
      id: 1,
      token: '123',
      name: 'name',
      contact: 'lorem ipsum',
    },
    {
        id: 1,
      token: '123',
      name: 'name',
      contact: 'lorem ipsum',
    },
]

to

// Format Object { id: '', token: '', identity: { name: '', contact: '' }, }

thanks

1 Answer 1

1

Use map:

const data = [{
    id: 1,
    token: '123',
    name: 'name',
    contact: 'lorem ipsum',
  },
  {
    id: 1,
    token: '123',
    name: 'name',
    contact: 'lorem ipsum',
  }
];

const newData = data.map(({ id, token, name, contact }) => ({
    id,
    token,
    identity: {
      name,
      contact
    }
}));

console.log(newData);
.as-console-wrapper { max-height: 100% !important; top: auto; }

Sign up to request clarification or add additional context in comments.

Comments

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.