Custom JSON filtering

I have a JSON object called Menu which contains various menu items for my system that I am developing using VueJS + Vuetify. I need to filter these menu items based on the "text" field, regardless of position in the text and without differentiating between upper and lower case letters, similar to SQL's like '%filter%' query. It would be ideal if accents are also disregarded but not crucial.

If the search pattern matches a child node, should the parent node also be included in the filtered results? Is it possible to achieve this filtering behavior using a JavaScript function?

The structure of the menu is as shown below:

https://i.sstatic.net/8Dt9N.png

Here is the expected behavior after filtering:

https://i.sstatic.net/Bz4KY.png

Answer №1

If you're looking to extract the text fields of child elements, one way to do it is by initializing all parent elements with empty arrays for children like this:

 childrenWithoutText = Menu.filter(function (item) {
  return item.children == null ;
}); 
 textFieldsExtracted = childrenWithoutText.map(({ text }) => text)

Answer №2

Learn how to use regular expressions to filter matches in an array.

var arr = [{
    icon: "add",
    text: "Cargar Pedidos",
    url: {
      name: "cargarpedidos"
    }
  },
  {
    icon: "playlist_add_check",
    text: "Aprobar Pedidos"
  },
  {
    icon: "content_copy",
    text: "Remitir Pedidos"
  },
  {
    icon: "content_copy",
    text: "Remitir Pedisddos"
  },
  {
    icon: "content_copy",
    text: "Remitir Pediados"
  },
  {
    icon: "content_copy",
    text: "Remitir asdaasd"
  }
];

var name = "dido";
var regex = "/" + name + "/g";
var filtered = arr.filter(
  (val) => val.text.match(name)
);

console.log(filtered);

These are the fundamentals of filtering. Use this function if you want to search for any property in an array object:

var result = Menu.filter(function(o) {
  return Object.keys(o).some(function(k) {
    return o[k].toString().toLowerCase().indexOf('dido') != -1;
  })
})

console.log(result)

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

VueJs Plugin for Adobe XD Development

I've been working with the "ui-hello-vue" example from the Adobe official Vue plugin repository. Initially, everything was functioning properly straight out of the box. However, I encountered difficulties trying to embed the Vue code into a panel. O ...

Error: The src for the image of the double cheeseburger could not be properly parsed by the `next/image` component

Could you kindly take a moment to review this? import Image from "next/image"; import { useState, useEffect } from "react"; import "@/data/data.json"; interface propsData { dish: { id: numbe ...

Is there a chart tool that allows for editing graphs by simply dragging points?

Currently, I am in search of a charting library that is preferably JavaScript-based (although Flash could work as well) and has the capability to display time series data as a line chart. Additionally, I need this library to allow users to drag points on t ...

I need help getting my Vue.JS project to function properly when it is deployed on a server

After creating a new VueJS project using the Vue UI, I noticed that everything runs smoothly locally at http://localhost:8080/ with no errors. However, when I build the project and upload the files from the dist folder to my hosting package via FTP, I end ...

Verifying the "select" dropdown option prior to final submission

My website features multiple select dropdowns that require validation before submission. For instance, there may be a situation where seven select dropdowns are present as described below. My desired validation criteria is: If there are 5 or more "sel ...

"Encountering a net::ERR_UNKNOWN_URL_SCHEME error message when making an Ajax post request

I'm encountering an issue while attempting to make a post call using Ajax from my frontend to my Express server. The error message I'm getting is net::ERR_UNKNOWN_URL_SCHEME. Here's the code snippet for the Ajax request: function sendStep ...

JS custom scrollbar thumb size issues in relation to the scroll width of the element

I recently implemented a custom scrollbar for a component on my website. To determine the length of the scrollbar thumb, I use the formula viewportWidth / element.scrollWidth;. This provides me with a percentage value that I then apply to the thumb elemen ...

What is the best way to display an image path and add it to the Ajax success function in a CodeIgniter application?

I am struggling to display my image path correctly using append and a variable to store the value. However, whenever I try, it results in an error. Let me provide you with the code snippet: <script type="text/javascript"> $(document).ready(funct ...

Updating a section of a webpage using jQuery Mobile

I'm currently facing an issue with modifying a specific section of my webpage. Although this problem has been discussed before, I haven't found a satisfactory solution yet. The element in red on the page needs to change dynamically based on the ...

Simulating an API request using Vue and Jest/Vue test utils

Utilizing Vue for the frontend and Python/Django for the backend, I aim to create tests that verify the functionality of my API calls. However, I am encountering difficulties when attempting to mock out the Axios calls. I suspect there might be an issue w ...

Parsing Retrofit responses in real-time

I'm struggling to parse a dynamic Retrofit response. The response structure includes multiple objects within Tracking History, each with a unique name. I attempted to create a list of objects inside the tracking history but faced challenges due to the ...

An error has occurred with the Firefox Addon: the module `path` cannot be located within the resource://gre/modules/commonjs/http.js

Currently developing a Firefox add-on on Windows10 with node v5.8.0 and npm v3.5.3, using Firefox v.45.0 The issue arises from the following line of code: var path = require("path"); The error message reads: Message: Module `http` is not found at resou ...

Getting rid of the border next to v-navigation-drawer in Vue.js

I need help removing a persistent thin line on the left of my navigation drawer that I created without borders. Despite trying to remove it using border : none, the line remains there. Upon inspecting the element, I discovered that the issue lies in a spe ...

The Ionic tab is already finished displaying even before the data has completed loading

Creating a favorites section for a vouchers app has proven to be slightly challenging. When attempting to retrieve the current user's favorite vouchers and display them using ngFor in the HTML, I encountered an issue. The very first time I open the fa ...

What is the best way to integrate Emotion styled components with TypeScript in a React project?

Currently, I am delving into TypeScript and attempting to convert a small project that utilizes Emotion to TypeScript. I have hit a roadblock at this juncture. The code snippet below export const Title = styled.div(props => ({ fontSize: "20px", ...

Retrieve items within an array of objects in MongoDB using an array of IDs (using the $in operator in aggregation)

In my MongoDB database, I have a collection of stores [ { "_id" : ObjectId("6043adb043707c034d5363b7"), "shopId" : "shopid1", "appId" : "777", "shopItems" : [ { ...

Transition and transition-group animations in Vue.js do not seem to be functioning correctly

Having trouble implementing CSS transitions between slides in a Vue 2 Carousel component. Slides are created using v-for and shown/hidden using v-show. Attempted to use <transition> with mode=out-in, but encountered issues with two slides displaying ...

Annoying jQuery animation error: struggling to animate smoothly and revert back. Callback function conundrum?!

I'm completely lost with what I have accomplished. My goal was to create an animation where an element slides in from a certain position and then slides back when another element is clicked. To achieve this, I included the second event within the call ...

Troubleshooting Vue and Typescript: Understanding why my computed property is not refreshing the view

I'm struggling to understand why my computed property isn't updating the view as expected. The computed property is meant to calculate the total price of my items by combining their individual prices, but it's only showing me the initial pri ...

Developing a standalone Vue component within an expansive project

I am currently working on a PHP project that utilizes Kirby CMS and Gulp for asset building. The challenge I am facing now is incorporating a complex calculator on the homepage using Vue, without introducing excessive new tools into my workflow. What I rea ...