Implementing new updates to an object without affecting existing values in Vue.js 2

I am currently facing an issue with updating values for an object received in a payload before saving it to the database. Despite making changes, the new values are not being persisted correctly. I am utilizing Vue.js 2 for this task. How can I effectively update the incoming object and store those updated values in an existing object?

Additional details: The data we receive from an API may contain existing key-value pairs or none at all depending on certain criteria such as username, birthday, or phone number. The goal is to update the personal information fields with any new values. However, instead of updating the new values, the data retains old changes without incorporating the updates. In this case, userPersonalInfo is not being updated.

ModalVerification.vue

onVerifySuccess(existingData) {
 // if no object exists, complete new form 
 if(!Object.keys(existingData).length) {
   this.completeFormModal();
  } else {
   this.meetingDetails.is_authenticated_user = true;
   this.updateMeetPlanInformation(this.getMeetingPlanFields(existingData));

   // only return existing data if object is not null; otherwise, update the existing data with new key/value pairs. This approach seems incorrect as it does not verify if any values have been updated before passing them.
   const userPersonalInfo = (existingData === null) ? this.getUserPersonalInfo(existingData) : existingData;

vueAssign(this.meetingDetails, userPersonalInfo);
this.completeFormModal();
}

export function vueAssign(objVal, srcVal) {
 Object.keys(srcVal).forEach((key) => {
   Vue.set(objVal, key, srcVal[key]);
 });
}

Answer №1

The issue may lie within the vueAssign function, but without seeing that method I can still provide some potential solutions:

Object.assign

You can utilize the Object.assign method to transfer properties from meetingDetails to userPersonalInfo, replacing any matching properties:

Object.assign(userPersonalInfo, this.meetingDetails)

const userPersonalInfo = {
  a: 1,
  b: 2,
}

const meetingDetails = {
  a: 999,
  c: 'hello',
}

Object.assign(userPersonalInfo, meetingDetails)

console.log({ userPersonalInfo })

Spread operator

Another option is using the spread operator, which achieves the same result:

let userPersonalInfo = /*...*/

userPersonalInfo = {
  ...userPersonalInfo,
  ...this.meetingDetails,
}

let userPersonalInfo = {
  a: 1,
  b: 2,
}

const meetingDetails = {
  a: 999,
  c: 'hello',
}

userPersonalInfo = {
  ...userPersonalInfo,
  ...meetingDetails,
}

console.log({ userPersonalInfo })

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 with the Cordova button not functioning on Android 9? A beginner seeks assistance

I attempted to create a button with the id of "clickme" that links to index.html and js/buttonexample.js. Although I could see the button on both the browser and android emulator, it wasn't functioning as expected when clicked. js/buttonexample.js d ...

What could be causing Next.js middleware to run repeatedly?

Recently, I set up a brand new Next.js project using npx create-next-app@latest --typescript. Upon completing the installation (version 13.3.4), without making any modifications to existing files, I introduced a new file named middleware.ts within the src ...

utilizing ng-option to present choices in a dropdown menu

I have been implementing a scroll-down menu in my application using options. However, I now want to switch to using ng-option to populate the values from a JavaScript file. I also require assistance with writing AngularJS code for this task. Below is a s ...

The modified navigation bar is not appearing in the display of the Laravel and Vue project

After creating a fresh Laravel project using the command laravel new projectname, I selected jetstream, inertia, api, dark, ssr as my preferences. However, when I made modifications to the navbar section in app.blade.php by adding an item on the left side ...

Is there a way to determine the number of clicks on something?

I'm attempting to track the number of times a click event occurs. What is the best method to achieve this? There are two elements present on the page and I need to monitor clicks on both of them. The pseudo-code I have in mind looks something like ...

Display the selection order in the index for multiple items in the Vuetify component v-select

Hey everyone, I'm new to Vue.js and I've encountered a roadblock with a basic issue. I created a multiple v-select (Vuetify component) that allows users to select one or more items from a list. <v-select dark class="mb- ...

Reveal unseen information on the webpage upon clicking a link

I have successfully implemented a fixed header and footer on my webpage. The goal is to make it so that when a user clicks on a link in either the header or footer, the content of that page should appear dynamically. While I have explored various styles, ...

Leveraging a fetch request response in componentDidMount to power a subsequent request within a React-Redux component

I am currently facing a challenge with a component that triggers a fetch request (in redux) on componentDidMount. I now need to make another fetch request in the same component using the response data from the first fetch, and ideally before rendering. Si ...

What is the method to create a resizable table column border rather than resizing the bottom corner border?

At the moment, we are able to resize the table border when we drag the corner. However, my goal is to enable resizing on the right side of the full border. https://i.sstatic.net/yFI24.png Below is the CSS code for resizing: th{ resize: horizontal; ove ...

Get your hands on a PDF containing server node and vue.js

I am facing an issue with creating a secure download link for a PDF file on the server. When clicking on the link, the file is not being downloaded properly. Ensuring that the PDF link remains hidden from the client but still allows for downloading direct ...

Creating interactive dropdown menus with PHP and Catalyst using Jquery

Currently, I am working on incorporating cascading dropdown menus into a catalyst web app. The main goal is to allow users to select a database table from the first dropdown menu and have the columns of that table populate the second dropdown menu. To achi ...

Step-by-step Guide: Building a Nested Bootstrap Navigation Tabs Component on a Webpage

Within a page, I have integrated nested bootstrap navs components. The issue arises when clicking on "Nested Tab 2," which functions correctly, and then switching to "Nested Tab 1" where the tab content is not displaying properly. I am uncertain if there ...

What is the best way to delete a CSS class from a specific element in a list using React?

I need to implement a functionality in React that removes a specific CSS class from an item when clicking on that item's button, triggering the appearance of a menu. Here is my code snippet. import "./Homepage.css" import React, { useState, ...

The issue with dynamic sizing in React and Tailwind is that it does not consistently work with arbitrary sizes. Some sizes do not appear properly, causing items to

In this code snippet, I am attempting to create a circle component of dynamically sized using html div and Tailwind CSS w-[diameter] & h-[diameter] attributes in a create-next-app@latest project. However, the circle fails to render properly with certa ...

The Shell Application is not refreshing React HtmlElement Micro Front end

I am currently facing an issue when trying to inject the following React MFE into another Angular shell application. The MFE loads successfully the first time, but if it is hidden or removed from the DOM and then reloaded, it fails to load. Could you plea ...

Eliminating the need for RequireJS in the Typescript Visual Studio project template

After integrating RequireJS into my Typescript template using the nuget package manager, I found that it was more than what I needed and decided to uninstall it. Even though I removed the package through nuget and the files were deleted properly, my Typesc ...

Refreshing a Thymeleaf table dynamically without having to reload the entire page

I am currently using the Thymeleaf attribute to render data, but I am now looking to add a "Search" button without reloading the page. Within the attribute departments, I am rendering a List<Department> from the database. While I understand how to a ...

Is there a way to verify the presence of multiple array indices in React with TypeScript?

const checkInstruction = (index) => { if(inputData.info[index].instruction){ return ( <Text ref={instructionContainerRef} dangerouslySetInnerHTML={{ __html: replaceTextLinks(inputData.info[index].instruction) ...

Is there a way for me to retrieve the value nested within an object within another object from this Api response?

Hey there, I'm currently struggling to retrieve the value of faceit_elo from within the csgo object. I attempted using data.games.csgo.faceit_elo but unfortunately it didn't yield any results. Any suggestions on how to access this value would be ...

"Using the power of jQuery to efficiently bind events to elements through associative

I am attempting to link the same action to 3 checkboxes, with a different outcome for each: var checkboxes = { 'option1' : 'result1', 'option2' : 'result2', 'option3' : 'result3', }; ...