Having trouble with your computed includes not working in Vue.js? Unsure of how to fix it?

How come when I use "includes" in Vue with my v-model, Vue.js logs an error? However, if I write (.includes("blabla)), everything works fine.

What could be the issue?

Also, how can I return the entire array if the (if) condition will work?

For example, something like this.emojiesByCategory.push() ?

Please assist me with this.

<template>
    <div id="app">
        <div class="emoji_picker">
            <div class="picker_container">
                <input v-model="search">
                <div class="category"
                     v-for="category in categories"
                     :key="`category_${category}`"
                >
                    <span>{{ category }}</span>
                    <div class="emojis_container">
                        <button
                            v-for="(emoji, index) in category_emojis(category)" // ERROR HERE
                            :key="`emoji_${index}`"
                        >
                            {{ emoji }}
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import data from '../data.json'

export default {
    name: 'App',
    data() {
        return {
            search: "",
            emojiesByCategory: ""
        }
    },
    computed: {
        categories() {
            return Object.keys(data);
        },

        category_emojis: () => (category) => {
            for (let i = 0; i < Object.keys(data[category]).length; i++) {
                if (Object.keys(data[category])[i].includes(this.search)) { // ERROR THERE !!!
                    console.log(Object.values(data[category])[i]) 
            }
            return Object.values(data[category])
        }
    },
}
</script>



// How do I fix this?

My JSON looks like:

{
    "Frequently used": {
        "confused": "😕",
        "neutral_face": "😐",
        "blush": "😊",
        "heart_eyes": "😍"
    },
    // and others
}

Please lend me a hand

Answer №1

Utilizing arrow functions in computed properties may result in the loss of access to this.search. When referencing this.search within an arrow function, it does not point to the Vue instance's data property search, but rather to the parent scope (potentially window in a browser environment), where search is likely undefined.

To simplify:

category_emojis() {
    return (category) => {
        let filteredEmojis = {};

        for (let emoji in data[category]) {
            if (emoji.includes(this.search)) {
                filteredEmojis[emoji] = data[category][emoji];
            }
        }

        return Object.values(filteredEmojis);
    }
}

Additionally, ensure that you initially declared emojiesByCategory as a string within your data. It should be changed to an array as well.

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

Managing embedded URLs in Next.js applications

I am currently in the process of developing an ecommerce platform, expecting users to utilize both our domain and their own custom domains. For example: ourplatform.com/username theirdomain.com My goal is to customize the inline links based on the speci ...

Meteor chat platform now offers the option to create multiple chat rooms

I've been working on an application that features multiple chat rooms. Currently, one of the rooms is functional in terms of sending and receiving messages. However, when I navigate to a different room and try to send a message, the message doesn&apos ...

What is the proper usage of main.js and main.css within the skeleton portlet project that is created by the Liferay 6.0.6 plugin SDK?

Is it a good practice to include portlet-specific JS or CSS in them only if <portlet:namespace /> works within them? Should I rely on unique function/variable names or class names instead? ...

What is the best way to create a transparent sticky header that blends with the background while still maintaining visibility over images and text?

On my web page, the background features a radial gradient but the content extends beyond the viewport, causing the center of the gradient to not align with the center of the screen, producing the desired effect. However, I'm facing an issue with a sti ...

Adjusting the color of a cell based on its value

Currently, I am in the process of converting a CSV file to an HTML table by utilizing a tool available at . However, I am facing a challenge in modifying the background color of cells based on their values. I would greatly appreciate any help or guidance w ...

What is the best way to write an SQL query to safely insert a record into a table with a dynamic name?

I'm working on a function that can insert a record into a table in PostgreSQL. The catch is that the table name needs to be a parameter for the function, and the column names are determined dynamically. To ensure protection against SQL Injection, I am ...

Issue arises when Protractor is unable to compare a variable with a web element due to unresolved promises

My strategy for this examination was to extract the inner text of an element, modify it, and then verify that the change had taken place by comparing the element with the variable. var fixedValue = element(by.xpath('/html/body/section/div/section/sec ...

What are the steps to integrate Webpack into an EJS Reactjs Express MongoDb application?

I have incorporated EJS, Express/NodeJs, and MongoDB into the project, along with ReactJs as an addition. My next step is to introduce Webpack for bundling both EJS and ReactJs files together. Although the end goal is to transition the p ...

Is it possible to locate and eliminate the apostrophe along with the preceding letter?

My objective is to tidy up a character string by removing elements that are not essential for the user and SEO, specifically the (letter before the apostrophes) in this case. I am looking for a regex solution or explanation of how to achieve this in PHP, a ...

Utilizing VueJs components within Django with the help of django webpack loader: A step-by-step guide

After following a tutorial on integrating Django and VueJs using django-webpack-loader, I successfully loaded the main.js output from django-webpack-loader to my index.html. Here is the structure of my project directory: - assets - bundles - app. ...

Converting the jQuery $.xajax loadmore feature into a custom XMLHttpRequest JavaScript function

I'm facing challenges while trying to create a XMLHttpRequest loadmore function as compared to using $.ajax. I am uncertain about what I might be missing in my code. Below is the function that is based on a previously working $.ajax version that I ha ...

Check if the content key Json exists by implementing Vue

Can anyone help me with checking the existence of "novalue"? For instance: { name: "maria", city_id: "novalue" .... } What would be the best way to do this in Vue? Should I use <div v-if="condition"> or a function? ...

What is the best way to check the difference between the current date and time with another date and

Seeking assistance in comparing 2 dates using JavaScript has been a bit challenging for me. I haven't found the exact solution on this platform yet. As a beginner in JavaScript, I initially assumed that obtaining the current date and time would be a s ...

Tips for adjusting the color of a <a> tag upon clicking, which remains unchanged until the URL is modified

My goal is to maintain the color of a link when it is clicked. Currently, hovering over the navbar link changes it to greyish, but I want it to remain a different color after clicking on it. I am implementing this using the react-router-dom library for the ...

How to extract user data from a JWT token using Node.js?

In my MEAN stack authentication application, I am utilizing Node and Angular. After a successful login, I set a JWT token and store it in a session within the controller by assigning the token to config.headers through a service interceptor: var token = j ...

Looking to display or conceal several divs according to the option selected from a dropdown menu?

I've been searching for a solution to my simple dropdown issue, but haven't had any luck in the forums. This is the code for the dropdown: <select id ="category_faq"> <option value="1">item1</option> <option value= ...

What is the technique for hiding the bottom tab navigator upon leaving a specific screen in React Native version 5?

In the home screen, I want only the bottom tab navigator to be visible, and then hidden until the user returns to the home screen. The example provided below is tailored for working in the App.js file, but my situation is different. const Tab = createBot ...

Presenting a ui-router modal while maintaining the parent state's refresh-free appearance

I have implemented a unique feature in my angular app called modalStateProvider. It allows me to easily have modals with their own URLs. // Implementing the modalStateProvider app.provider('modalState', [ '$stateProvider', function ...

What purpose does the "io" cookie serve in Socket.IO?

Can someone explain the purpose of Socket.IO using the io cookie as a session cookie? I understand that it can be disabled, but I couldn't find any information on it in the documentation. Why is it turned on by default and what consequences would ther ...

My socket io connection is not working. There seems to be an issue with the connection io

After initiating my server, I am able to see the console.log message showing that the server is running on the specified port. However, I am not receiving the console.log message indicating a successful socket io connection with a unique socket id. import ...