Refresh the array using Composition API

Currently, I am working on a project that utilizes Vue along with Pinia store.

export default {
  setup() {
    let rows: Row[] = store.history.rows;
  }
}

Everything is functioning properly at the moment, but there is a specific scenario where I need to modify and filter the array:

 const filterArray = () => {
      rows=store.history.rows;
       for (let index = 0; index < rows.length; index++){
        if (rows[index].department !== departmentModel.value) {
           rows.splice(index, 1);
        }
      }
    };

However, it seems like the filterArray method is not only filtering the `rows` array but also impacting the `store.history.rows` array. Consequently, both arrays end up empty quickly. My objective is to refresh the `rows` array every time the `filterArray` function is executed by replacing it with the entire content of the `store.history.rows` array and then implementing the necessary filtration based on the condition.

Can someone guide me on what might be going wrong in my current implementation?

Answer №1

When you assign rows = store.history.rows, it does not create a duplicate of the array. Instead, it functions as a reference to the original array.

To avoid this behavior, you should first make a copy of the array before making any modifications to it.

rows = [...store.history.rows];

Alternatively, you can follow functional programming conventions, which I believe is the recommended approach.

const filterArray = () => {
  rows = store.history.rows.filter(item => item.department === departmentModel.value)
}

This method will generate a new array containing items that match the specified departmentModel.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

MUI options - The specified type 'string' cannot be matched with type '"icon" | "iconOnly" | "text" | "outlined" | "contained" | undefined'

Is it possible to utilize custom variants in MUI v5? I am having trouble using a custom variant according to their documentation: https://mui.com/material-ui/customization/theme-components/#creating-new-component-variants declare module "@mui/material ...

jqgrid's date restriction is set to November 30th, 1999 at midnight

I have a table displaying DATETIME values. However, after editing the datetime value, it always changes to "1999-11-30 00:00:00", both in jqgrid and the database, regardless of the date entered. [Tue Mar 12 11:39:28 2013] [error] [client 171.43.1.4] PHP N ...

Swap two frames effortlessly with just a single click!

Is there a way to effortlessly change the contents of two frames with just one click? With a single click, I'd like to modify both "framename" and "framename2" by setting their href attribute to 'qwerty' and 'qwerty2' respectively ...

Tips for efficiently querying an array of objects with dynamic search criteria in VueJs3?

I have a search feature that scans through Objects and displays results based on the chosen search parameter (ID, NAME). While searching by ID works perfectly, trying to search by name returns undefined. I'm puzzled as to why ID can be successfully s ...

can a computed property be delayed in its calculation?

Within the code snippet below, we can see that in the compPropsIsBtnDigitizePolygonDisabled function, it initially checks if the digitizePolygonInteractions variable is initialized. If it is not initialized, an error will be triggered. During execution, w ...

The find function within $(this) is malfunctioning

I'm having issues with displaying/hiding content when clicking on a table row. Here is the simplified code snippet: HTML: <table> <tr onclick="showDetails()"> <td>Some text <br> <span class="hiddenC ...

Sending various values to a JavaScript function

I am working with a function that looks like this: //Function Call with Single Parameter responses(baseURL); //Function Definition function responses(baseURL) { $.ajax({ url: baseURL, type: "get", cache: false, header ...

What is the process for using AJAX and PHP to upload an image file?

I'm facing an issue where I want to insert an uploaded image into a database with user details for a profile picture. The output I receive currently shows {"current_field":null,"field_count":null,"lengths":null,"num_rows":null,"type":null}. How can th ...

development of MapLayers with rails and javascript

As a newcomer to RoR, I am encountering an issue that seems to be eluding me. I attempted to replicate the example application found on the mapLayers GitHub repository at https://github.com/pka/map_layers/wiki. However, all I see is the JavaScript code gen ...

Is it possible to use jQuery to load an HTML file and extract its script?

I have a navigation bar with five buttons. Whenever a button is clicked, I want to dynamically load an HTML file using AJAX that includes some JavaScript code. The loaded file structure resembles the following: <div> content goes here... </div& ...

How can Vue.js update the displayed information when the selection option changes?

Ensuring the total weight of the products remains accurate when changing the unit of measurement is essential. Currently, although the data in the array is being updated, these changes are only reflected on the screen after clicking on other input fields. ...

HAML Error: $ is not defined - Uncaught ReferenceError

I am encountering an issue with the following view: /views/admin/home/index.html.haml = render partial: 'general_tab_partial' .box.boxtab %article %h2= _('Global Reporting') .clearfix = form_tag '#', :method = ...

Is there a way to replace null values with empty strings when using json_encode?

I'm struggling to change null values to empty strings in the output of my json_encode: if ($uresult->num_rows >0) { while($urow = $uresult->fetch_assoc()) { $rresult = mysqli_query($con,"SELECT * FROM allid WHERE postid='$oldi ...

The requested function is nowhere to be found within the confines of my Controller module

While working on a personal project, I encountered an issue where a function from another class in a separate Java file is not being found, even though it is defined in the respective class. EventView.js: displayEvent(event){ this.EventTitle = event. ...

The Next.js application encounters a crash when trying to integrate Google Firebase authentication

I'm encountering an issue while trying to integrate authentication using firebase (via Google) in my next.js application, and the app crashes consistently. I will provide the code for the auth.js page component, as well as where I set up firebase and ...

Implementing Asynchronous context tracking within a Remix application utilizing Express as the server

Utilizing Remix with Express as the server, I aim to develop an Express middleware that establishes an async context to grant all downstream functions (especially those in the "backend" Remix code) access to this context within the scope of a single reques ...

What are the steps for loading JSON data into a select dropdown with the help of AJAX?

I am trying to create a dropdown list of schools using the select tag. Currently, I have hard coded values for the list, but I want to fetch the data from a RESTful service instead. Can someone please provide guidance on how to achieve this? <html& ...

The jqGrid colModel is failing to execute a function as expected

Within the given code snippet, the function attrSetting is invoked. However, when I modify it to {"name":"A", "index":"0", "cellattr":attrSetting}, the code executes smoothly. But here lies the issue - cellattr interprets it as a string rather than a fun ...

What could be the reason behind the material table not populating with data from the source, despite the service returning an array?

Currently, I am utilizing a mean stack in order to craft a bug tracking system. The issue arises when my express.js service returns an array of issues, which I assign to another array that functions as the dataSource for mat-table. However, despite the ar ...

Creating nested directories in PHP using arrays: A step-by-step guide

When working with Laravel, I encountered an issue while trying to create a trait file using the console. The problem arises when specifying a directory for the file creation - it does not get created as expected. The structure of the directory can be dyna ...