Using Vue.$set, you can create nested objects that were previously missing in your

Is there a way to generate missing nested objects (such as bar, a, b, and c) when employing Vue.$set in the following manner?

export const mutations = {
  UPDATE(state, payload) {
    this._vm.$set(state.foo.bar.a.b.c, payload.key, payload.value)
  }
}

Instead of using state.foo.bar.a.b.c, I'm actually utilizing _.get(state, payload.path).

Answer №1

Based on the code snippet you provided, it seems like it should work fine. Have you encountered any errors while using it?

UPDATE:

After reviewing your example, it appears that there may be instances where the variable b does not exist and needs to be created if so.

Although Vue.$set may not handle this automatically, you can implement a custom solution like the one below. This function should properly create any missing objects within the chain of payload.path:

      let objectLevels = payload.path.split('.');
      const propToChange = objectLevels.reduce((res, level) => {
        if (!res[level]) this.$set(res, level, {});
        return res[level];
      }, state);
      this.$set(propToChange, payload.key, payload.value);

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

Retrieval and sorting of documents by their unique identifiers

There are approximately 20 documents stored in the Firebase database, each containing fields with unique "id" values. How can we filter the database query to only retrieve documents with a specific "id" and exclude all others? ...

KineticJs layer malfunctioning after stage reconstruction

My canvas is created using KineticJs Stage, with three layers - a background that is always visible and two overlays toggled by a checkbox. Whenever the parent div of this stage is resized, I redraw the entire stage to maintain the layout. The toggling wor ...

Is it possible to modify the host header within an Angular app?

I'm experiencing a vulnerability issue and to resolve it, I need to utilize SERVER_NAME instead of the Host header. Is it possible to accomplish this using Angular? ...

A collection of deep, dark faces devoid of reflection, showcasing the power of MeshStandardMaterial within

I am new to three.js and the realm of 3D graphics. My goal here is to delve into the learning process. My aim is to showcase a dodecahedron with a metallic appearance, but I'm encountering an issue where about half of the faces are appearing black an ...

Optimizing CSS usage within Django: top recommendations

Throughout a 6-month-long project, I have continuously added new URLs. However, I am now encountering an issue when using the extend 'base.html' function on other pages where the CSS overlaps and creates confusion. My question is: What are the b ...

Retrieve information from an array of objects by utilizing a separate array

There are two separate arrays provided below: ages = [20,40,60]; doctors = [{ "name": "Moruu", "age": 18, "sex": "Male", "region": "Africa" }, { "name": "Khol ...

Error message: "Attempting to update /mytable using the `UPDATE` SQL command on React App localhost:MYPORT with react-postgres and PostgreSQL / node.js backend is not allowed."

Following the tutorial located at https://github.com/nsebhastian/react-node-postgres, I have successfully implemented the full guide, as evidenced by the results documented in TypeError: Cannot read property 'rows' of undefined. The functionalit ...

Transmitting C# data to a JavaScript script through JSON serialization. Issue encountered: Character invalid

I'm facing an issue with sending data from my Entity Framework database to a JavaScript script on my webpage. Below is the snippet of code from my MVC Controller: public ActionResult Index() { var wordsToShow = db.Words.Where(w => w.O ...

Is utilizing getStaticProps the best approach for handling offline data in Next.js?

My current project in Next.js involves offline static data for the webpage content. I am using an array to store the data for later mapping. Do you recommend using getStaticProps in this scenario? For example, here is a snippet of my code: import { data } ...

prismjs plugin for highlighting code displays code in a horizontal format

If you're looking for a way to showcase source code on your website with highlighted syntax, prismjs.com may be just what you need. It offers a similar style to monokai... However, I've encountered an issue where the plugin displays my code in a ...

modify the color of the selection based on the chosen option in Vue

Is there a way to change the text color to red in a select element in Vue.js when a specific option is chosen? Below is the select element where I need the text color to change, while still keeping its value as null. <select> <option value=&q ...

Adjust text at multiple positions using Javascript

I am working on parsing a complex query and developing a tool using the following sample: var str =" SELECT 'Capital Return' AS Portfolio_Classification , '(Gross)' AS Portfolio_Type , '0.21%& ...

The Axios post request is mistakenly sending my data as the key with an empty value instead of sending the entire data object itself

I began by developing the frontend, but now I am looking to create the backend in order to establish a connection with a database. const express = require("express"); const bodyParser = require("body-parser"); const cors = require(" ...

react function for class activation

Can you guide me on how to trigger the activation of the element's class when clicking the 'Next' button? The goal is for the styles to change upon clicking a specific class. Any advice on integrating this functionality within the provided f ...

Tips for integrating Vue 3 with Nuxt:

Currently working on a project with Nuxt3, but the cli installation from the docs resulted in a vue 2.7 project creation. Any recommendations on how to switch to using vue 3? ...

form_not_submitting_ajax

In the app I'm working on, I have a form that is defined in the following manner: = form_with model: project, remote: true, method: :put do |f| = f.select :selected_draw, options_for_select(project.draws.pluck(:number, :id), draw.id), {}, class: &a ...

Instructions on enabling checkbox functionality within a dropdown menu using BootstrapVue.js

Can someone help me troubleshoot this dropdown issue in bootstrapvuejs? I'm having trouble checking the boxes before the dropdown closes. Any suggestions on how to fix this? <template> <div class="inventory-filter-button"> ...

Experiencing an issue with establishing a secure SSL/TLS connection while utilizing Test Complete CLR Bridge alongside TestRail .Net bindings

I am currently utilizing TestRail API .NET bindings in conjunction with JavaScript and TestComplete CLR bridge functionality. I have developed the Guorok.Testrail library by referencing the Newtonsoft.Json library in Visual Studio. In TestComplete, I can s ...

Moving punctuation from the beginning or middle of a string to the end: A guide

My Pig Latin converter works well with single or multi-word strings, but it struggles with punctuation marks. For example, when I input translatePigLatin("Pig Latin.");, the output is 'Igpay Atin.lay' instead of 'Igpay Atinlay.'. How c ...

Prevent the propagation of a particular CSS class's inheritance

I need to make modifications to an existing HTML5 app that features two unique themes. Here's an example of the current structure: <body class="theme1 theme2"> <div id="div1">I'm satisfied with both themes</div> <di ...