Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge object with Vuejs and Axios

I have two objects obtained from an axios.get and a fetch from an rss feed and the result is something like that: Two Object

Objects have fields with different names but with the same meaning. For example: Title and Full_name refer to the name of the object.

Currently i have this configuration:

Obj1 = {title: "Main text 1", body: "text text 1"},
       {title: "Main text 2", body: "text text 2"};
Obj2 = {full_name: "Main Text 3", description: "text text 3"};

I would like to get an object like this:

Obj3= {name: "Main text 1", desc: "text text 1"},
      {name: "Main text 2", desc: "text text 2"};
      {name: "Main Text 3", desc: "text text 3"};

Currently I am using this code:

<script>
    export default {
        data() {
            return {
                obj1: {},
                obj2: {} 
            }
        },
        mounted() {
            this.axios
                .get('https://url.com')
                .then((response) => {
                    this.obj1 = response.data;
                  });
            fetch('https://url2.com')
                .then((res) => res.json())
                .then((response) => {
                    this.obj2 = response.items; 
                });
        } 
    }
</script>

I tried these solutions has the result is always empty:

let merged = {...this.obj1, ...this.obj2};

var merged = Object.assign( {}, this.obj1, this.obj2);
like image 904
Francesco Mansi Avatar asked Nov 30 '25 02:11

Francesco Mansi


1 Answers

According to your screenshot, this.obj1 and this.obj2 are Arrays

So you can merge them with the spread operator by using brackets to create a new array:

let merged = [...this.obj1, ...this.obj2]
like image 160
Sovalina Avatar answered Dec 03 '25 05:12

Sovalina