[Vuejs]-How to merge a object to a array object in vuejs

3👍

Result is actually a list. You can use the destructuring assignment

  const result = [{id: 1, name:"Test 1"},{id: 2, name:"Test 2"}];
  const placeholder = {id:0, name: "[Select]"};

  const newObject = [ placeholder, ...result ];

to prepend the placeholder.

To use concat you must wrap the placeholder in a list.

[placeholder].concat(result)

to achieve the same result in a "more conservative way".

Or you can use the Object.unshift as suggested by @kemicofa ghost.

result.unshift(placeholder)

0👍

Obviously, Vue is a JavaScript’s progressive framework so we can use the same functions of JavaScript here to merge the objects in Vue.

Please use the following feature on your code!

Object.assign(target, Obj1, Obj2, ...);

Albert

👤Albert

Leave a comment