I have a api response.data
JavaScript
x
2
1
[{id: 1, name:"Test 1"},{id: 2, name:"Test 2"}]
2
And my component .vue
JavaScript
1
10
10
1
2
created() {
3
const request = axios.get("***api_url***").then(response => {
4
const result = response.data;
5
const placeholder = { 'id': 0, 'name': '[Select]' };
6
const newObject = result.concat( placeholder.map( item => ({id: item.id, name: item.name}) ) );
7
console.log(newObject);
8
});
9
}
10
I want merge placeholder object to result after api response
Advertisement
Answer
Result is actually a list. You can use the destructuring assignment
JavaScript
1
5
1
const result = [{id: 1, name:"Test 1"},{id: 2, name:"Test 2"}];
2
const placeholder = {id:0, name: "[Select]"};
3
4
const newObject = [ placeholder, result ];
5
to prepend the placeholder.
To use concat you must wrap the placeholder in a list.
JavaScript
1
2
1
[placeholder].concat(result)
2
to achieve the same result in a “more conservative way”.
Or you can use the Object.unshift
as suggested by @kemicofa ghost.
JavaScript
1
2
1
result.unshift(placeholder)
2