Francesco Mansi
Francesco Mansi

Reputation: 33

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);

Upvotes: 3

Views: 10536

Answers (4)

Sajib Khan
Sajib Khan

Reputation: 24156

The problem is your code var merged = Object.assign( {}, this.obj1, this.obj2); is executing before updating obj1 or obj2 by API response.

You can watch obj1 & obj2. Update the merged object when any of obj1 or obj2 is changed.

<template>
  <div>Merged Object: {{ merged }}</div>
</template>

<script>
export default {
  data() {
    return {
      obj1: {},
      obj2: {},
        merged: {}
      }
    },
  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; 
        });
  },
  watch: { 
    // whenever obj1 changes, this function will run
    obj1(newVal, oldVal) {
      this.merged = Object.assign( {}, newVal, this.obj2);
    },

    // whenever obj2 changes, this function will run
    obj2(newVal, oldVal) {
      this.merged = Object.assign( {}, newVal, this.obj1);
    }

  }
}
</script>

Upvotes: 0

Sovalina
Sovalina

Reputation: 5609

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]

Upvotes: 4

Radu Diță
Radu Diță

Reputation: 14171

You should map the second response and then concat to the first one.

var concObject = obj1.concat( obj2.map( item => ({name: item.full_name, desc: item.description}) ) )

NB: From your example obj1 and obj2 are arrays.

Upvotes: 0

Mohsin Jillani
Mohsin Jillani

Reputation: 81

Create a new Array Name as

var newArray[]
var Obj1 = {title: "Main text 1", body: "text text 1"},
           {title: "Main text 2", body: "text text 2"};
var Obj2 = {full_name: "Main Text 3", description: "text text 3"};`enter newArray.Push(Obj1);
newArray.Push(Obj2);

Upvotes: 0

Related Questions