What is the best way to filter through JSON data in Vue.js?

I'm encountering an issue with my JSON file that I am rendering to a table. I have 11 columns including id, name, and more. I want to search by every column, but the functionality only works for one column. If I try to filter the data multiple times, such as sorting by id and then by name, it crashes.

I am using a v-for structure to render data from the JSON file.

<div class="document__json" v-for="(item,index) in filteredJson" :key="index">
  <div class="document__data" :title=item.priority>{{item.priority}}</div>
  <div class="document__data" :title=item.refid_number>{{item.refid_number}}</div>
</div>

I attempted to search through the JSON using v-model, where every letter/word is sent to an array.

data() {
  return {
    myJson: json,
    search: []
  };
},

filteredJson: function() {
  let new_json;
  return this.myJson.filter((x) => {
    new_json = x;
    console.log(x);
    for (let i in this.search) {
      console.log(new_json[i])
      console.log(this.search)
      new_json = new_json[i].toLowerCase().match(this.search[i].toLowerCase());
    }
    return new_json
  });
}

<input type="text" v-model="search['priority']" class="document_search">
<input type="text" v-model="search['refid_number']" class="document_search">

When attempting to filter the JSON by comparing with data inside the search array, it only works if I search by priority. If I try to also search by refid_number, it crashes with the error: "Cannot read property 'refid_number' of null".

Answer №1

When working within your filter function, you initially assign new_json = x (which is an object). However, within the for loop, you then overwrite new_json with the result of a match, which is an array. This causes an issue because the array does not contain the same members as the original object x, leading to a potential crash in the subsequent loop.

To resolve this issue, you might consider implementing the following approach:

return this.myJson.filter((x) => {
    let match = true;
    console.log(x);
    for(let i in this.search){
        console.log(x[i])
        console.log(this.search)
        if(!x[i].toLowerCase().match(this.search[i].toLowerCase())) match = false;
    }
    return match;
});

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

How to locate the cell with a specific class using JavaScript/jQuery

I need help with the following code snippet: var output = '<tr>'+ '<td class="class1">One</td>'+ '<td class="selected">Two</td>'+ '<td>< ...

during implementation of ng-repeat directive with JSON dataset

When I receive JSON data and attempt to display it using the ng-repeat directive, I encounter an error ng-dupes error <table> <tr ng-repeat="emp in empArr"> <td>{{emp.empcode}}</td> <td>{{emp.empName}}< ...

Javascript - Execute function after all nested forEach loops have finished running

I'm finding this task to be quite challenging, as I am struggling to comprehend it fully at the moment. The issue involves nested forEach loops, and I require a callback function to execute once all the loops have finished running. I am considering u ...

Guide to establishing a connection to the Companies House API: Essential guidelines on setting up cURL headers and requisite API key specifications

I am attempting to establish a connection with the UK Companies House API, preferably using JavaScript. However, I am currently trying to set up this PHP version. How can I obtain access to the API using an API key? PHP: public function GetCompanyHouse( ...

What is causing me to repeatedly encounter the 403 forbidden error?

I keep encountering a 403 forbidden error and I am unable to retrieve my JSON data at all. Here is an excerpt from my HTML source: <html> <head> <meta name="keywords" content="jquery,ui,easy,easyui,web"> <meta name="descriptio ...

The step-by-step guide on passing arguments and fetching results from Angular UI Bootstrap Modal through Components

I am facing a simple scenario where I have defined a modal as a component and opened that modal using `uiModal.open`. However, when I try to pass custom data to the modal using "resolve" in the open method and arguments in the controller constructor, the d ...

When using a Webhook on Parse.com's after_save function, the resulting JSON data

Upon reviewing my Parse.com Error logs, I came across the following error: [E2015-09-28T12:40:37.531Z]vWEB after_save triggered for MPPChatRoom for user gI8UxW2JNa: The input causing the issue is as follows: {"object":{"counter":0,"createdAt":"2015-09-18 ...

What is the best way to incorporate npm packages into my projects?

Lately, I've been heavily relying on nodejs, but I keep running into the same issue. With so many projects available and a plethora of npm packages to choose from, it's frustrating that every time I try npm install --save some-package, I struggle ...

Error: Jest and Vue3 are unable to find the definition for ShadowRoot

I've been working on setting up Vue3 and Unit Test with Jest. It's been a struggle to get it functioning, and I've tried numerous configurations already. After trying many setups, this is the latest configuration that seems to be "working, ...

Tips for altering the appearance of a button when moving to a related page

I have a master page with four buttons that have a mouse hover CSS property. Each button's corresponding response page is defined on the same master page. Now, I want to change the button style when the user is on the corresponding page. How can this ...

Tips for Creating JavaScript Code for HTML Helpers in ASP.NET MVC

My HTML helper includes a textBox that looks like this: @Html.TextBox("txt1") <br /> I am interested in triggering a javascript onchange event on this textbox. Can this be accomplished, or would it be better to use an HTML input type instead? ...

Having trouble getting card animations to slide down using React Spring

I am currently learning React and attempting to create a slide-down animation for my div element using react-spring. However, I am facing an issue where the slide-down effect is not functioning as expected even though I followed a tutorial for implementati ...

Node is currently posing a challenge for the installation of packages

I am currently facing an issue while setting up my raspberry pi 3. I am attempting to install and execute a node code, but I encountered a problem during the installation of packages using npm. After trying multiple times with different versions of node ( ...

It's incredibly frustrating when the Facebook like button is nowhere to be found

Currently, I am in the process of developing a website for a local bakery and have decided to incorporate a Facebook like button on the homepage. After visiting developers.facebook, I proceeded to copy and paste the code provided. It appears that there use ...

Checking capabilities - Transfer document

I am currently facing an issue with the .setFilesToUpload method while testing an upload screen. Despite trying different approaches, I am unable to successfully upload any files. Upon further investigation, I noticed that selecting a file through the deb ...

JavaScript's GET method fails to retrieve the complete JSON file

I am facing an issue with a substantial JSON file that my JavaScript code is unable to pull in entirely. When I check the Network tab in Firefox developer tools, it shows that the data stops at around line 57,301 out of 528,342 lines in the JSON file. Ini ...

Utilizing a Function Across Controllers in AngularJS: A Guide

When developing my angularjs application, I decided to create two separate modules - 'home' and 'templates'. I am now faced with the challenge of utilizing functions from one module in the other. Here's how I approached it: Modu ...

Using two nested foreach loops in PHP, you can extract JSON content to access multiple nested nodes at various depths

I have a JSON file that I am attempting to import into a MySQL database table. Here is an excerpt from the JSON file: "categories": [{ "id": "cat0101000", "name": "TVs", "url": "https://www.url.com/site/", " ...

I'm having trouble retrieving data from the server using the AngularJS $http.get() function. What am I doing wrong

Ensure that your question is clear and that your code properly showcases the issue at hand. Feel free to leave any questions or comments below for clarification. app.js var express = require('express'); var app = express(); app.use(express.sta ...

Ways to customize parent element when child has a specific class in Vue?

What is the best way to style a parent div if the child has a specific class? I attempted using computed properties with $refs, but I always get an undefined value for $refs.[class] Example <div :style="isChildHasClass" class="container"> < ...