Struggling to pass two objects as props and have them be equated to one another

I have a scenario where I am passing a worker object as a prop to the view.vue field. In the mounted method of the component, I am trying to assign this worker object to the manager object. However, when I use the assignment `this.manager = this.worker`, it does not work. But if I manually assign each field like

this.manager.contact.first_name = this.worker.contact.first_name
this.manager.contact.last_name = this.worker.contact.last_name
this.manager.address = this.worker.address

Rather than manually assigning each field, I want to simply set the manager object equal to the worker object. Can someone please help me identify where I am going wrong?

Here is the code snippet:

<template>
 <v-data-table
  :headers="headers"
  :items="availabilities"
  hide-actions
  :disable-initial-sort="true"
  class="office-table"
  >
  <template slot="items" slot-scope="props">
   <td>{{ props.item.first_name }}</td>
   <td>{{ props.item.last_name }}</td>
   <td>{{ props.item.address }}</td>
   <td>
     <div>
       <a class="" href='javascript:void(0);'
        slot="activator"
        @click="assignmentValue(props.item)">
        <i class="material-icons add">add</i>
       </a>
     </div>
   </td>
  </template>
 </v-data-table>

 <UserForm :worker="user"></UserForm>
</template>

<script>
  import UserForm from 'user/view.vue'
  export default {
    components: {
      UserForm
    },
    data: function() {
      return {
        user: {
          address: '',
          contact: {
            first_name: '',
            last_name: '',
          }
        },
        availabilities: []
      }
    },
    created() {
      this.fetchAvailabilities();
    },
    methods: {
      fetchAvailabilities() {
        var that = this;
        this.$axios.get('availabilities.json')
        .then(response => {
          that.availabilities = response.data;
        });
      },
      assignmentValue(worker) {
        this.user.contact.first_name = worker.first_name;
        this.user.contact.last_name = worker.last_name;
        this.user.address = worker.address;
      }
    }
  }
</script>

View.vue

<script>

  export default {
    props: ['worker'],
    data: function () {
      return {
        manager: {
          contact: {
            first_name: '',
            last_name: '',
            email: '',
            phone: ''
          },
          address: '',
        },
      }
    },
    mounted() {
      this.manager = this.worker
    },
  };
</script>

Answer №1

To replicate the values from the worker object to the manager object, you have two options: either utilize Object.assign or employ the object spread syntax. This method works best when both objects share the same structure.

Here's an example:

this.manager = Object.assign(this.manager, this.worker);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble integrating Ionic Native API and Javascript API in one application?

Currently, I am developing an application using Google Maps that utilizes both the Ionic Native Google Maps API and the JavaScript version. The Native API is implemented on a page called home.ts, while the JavaScript API is utilized on a sub-page called de ...

Deriving worth from a JSON object

My JSON data structure has the following format: .... "location" : { "lat" : 37.42140090, "lng" : -122.08537010 }, .... I am having trouble accessing the lat and lng values. Any suggestions on how to do this? Cu ...

Adding the Setting component to the Style Manager

I'm struggling to add component settings (traits) into the Style Manager panel section because of insufficient documentation. The way it is demonstrated in the demo is not clear to me. Is there a specific block of code that I should be using? I' ...

Executing Code Upon Module Load and Presenting It as Middleware

I am currently delving into the intricacies of how exporting and importing modules function in Nodejs. One of my tasks involves using a specific file to seed a mongodb database. Surprisingly, this file operates flawlessly and delivers the desired results ...

Revamp MUI class names with React Material UI's innovative randomization and elimination

Can names be randomized or Mui-classNames removed? https://i.stack.imgur.com/J6A9V.png Similar to the image displayed? (All CSS class names would be jssXXX) Appreciate your assistance. ...

Is there a way to adjust the contents of an iframe to match the dimensions of the iframe itself?

I am trying to adjust the width of an iframe to 60%, regardless of its height. Is there a way to "zoom in" on the contents of the iframe to fit the width, so that I can then set the height based on this zoom level? I haven't been able to find any solu ...

jQuery mobile : accessibility helper for hidden elements

Within my HTML structure: <div data-role="fieldcontain" id="containdiv" class="no-field-separator"> <label for="field1" class="ui-hidden-accessible">To</label> <input type="search" name="field1" id="field1" autocorrect="off" a ...

Verify if the program is operating on a server or locally

My current project involves a website with a game client and a server that communicate via sockets. The issue I'm facing is how to set the socket url depending on whether the code is running on the server or my local PC. During testing and debugging, ...

Require assistance with refreshing the index for the chosen row

I have encountered a problem while attempting to manipulate table rows using Javascript. Adding new rows works fine, but deleting rows presents an issue. Specifically, the delete function fails if the first row or a row in the middle is deleted (the live ...

Using Vue3 to conditionally display a child div based on the class of its parent div

Just starting out with Vue, I'm currently experimenting with the active classes feature from this Vue3 carousel plugin. My goal is to only show the text and link divs for the carousel__slide--active class, but at the moment, all carousel__items are di ...

Print custom jQuery attribute to console or log output

I need help retrieving and displaying a custom jQuery attribute in either an alert or console. Despite attempting to use console.log() to access my custom data attribute, I am unable to see its value appear. The specific value I am trying to display is d ...

Exploiting jQuery UI in a Web Browser Bookmarklet

When using CoffeeScript, even though the code is very similar to JavaScript: tabs_html = "<div id='nm-container'><ul><li><a href='#tabs-1'>Guidelines</a></li><li><a href='#tabs-2'& ...

Avoid connecting HTML input elements with both JavaScript and Java models

I am troubleshooting an issue with my login page code. <div ng-show="!loggedIn()"> <form ng-submit="login()"> Username: <input type="text" ng-model="userName"/> Password: <input ...

What is the process for configuring NextJS to recognize and handle multiple dynamic routes?

Utilizing NextJS for dynamic page creation, I have a file called [video].tsx This file generates dynamic pages with the following code: const Video = (props) => { const router = useRouter() const { video } = router.query const videoData = GeneralVi ...

Utilize the map function on an array object to present data in a table using React framework

My parent component passes an array object that looks like this: I want to organize these values into a table with the keys in one column and their corresponding values in other columns. The desired format is shown here: To achieve this, I am currently i ...

getting a null response when using the map feature - coding battles

Given an array filled with integers, my goal is to generate a new array containing the averages of each integer and its following number. I attempted to achieve this using the map function. var arr = [1,2,3,4]; arr.map(function(a, b){ return (a + b / ...

Can access parent refs when exporting a child component with withStyles by following these steps:

For example: Child Component Code // Assume there is a styles object 's' being used in this component class Child extends Component { render() { return ( <h1 ref={(ref)=>{this.ref = ref}}>Hello</h1> ); } } export defau ...

What steps should I follow to transform SRM into L*a*b* values using the E-308 algorithm?

I'm grappling with the application of ASTM E-308 to SRM measurements in beer. The project I'm working on necessitates a reliable conversion from SRM to RGB (or sRGB) through the Lab* route. It's clear that each site I visit for beer recipe c ...

Guide on placing stickers on an item in Three JS

Recently, I began experimenting with the three.js library and have a question about decals: I managed to create a sphere with a texture applied to it. Is there a way to overlay another texture on specific areas of the sphere without repeating the original ...

Issue with updating the vertices in three.js EdgesGeometry is causing the Line Segments to not be updated as expected

I have created multiple three.js objects. I have observed that the 'other' objects, designed as Mesh/Geometry/Material, update as expected after calling verticesNeedUpdate() Furthermore, I have two wireframe objects that were designed in this m ...