Vue - Utilizing mapState in Vuex to display the contents of the first object within an array

I am trying to display the names array from the first object in players using mapState with Vuex. Currently, the objects in players are listed based on their titles, but I want to filter them based only on the names in the first object for the current page. Similarly, on the second page, I want to list them based on the names that will be added to the second object. I hope I have clearly explained my issue. How can I achieve this using a filter? Or is there a better way to accomplish this?

Players.vue

<template>
  <div class="Players">
    <CostumText class="Players-title" tag="h1">Clubs</CostumText>
    <div class="Players-search">
      <input type="text" v-model="search" placeholder="Search club.." />
      <label>Search player:</label>
    </div>
    <div class="Players-inner">
      <router-link
        :to="players.pathName"
        class="Players-inner-wrapper"
        v-for="players in filteredList"
        v-bind:key="players.id"
      >
        <div class="Players-inner-cards">
          <Clubs class="Players-inner-cards-svg" v-bind:name="players.id" />
          <CostumText tag="strong" lang="tr" class="Players-inner-cards-text">
            {{ players.title }}
          </CostumText>
        </div>
      </router-link>
    </div>
    <router-view />
  </div>
</template>

<script>
import { mapState } from 'vuex'
import CostumText from '@/components/CostumText'
import Clubs from '@/components/Clubs.vue'

export default {
  name: 'Players',
  components: {
    CostumText,
    Clubs
  },
  data() {
    return {
      search: ''
    }
  },
  computed: {
    ...mapState(['players']),
    filteredList() {
      return this.players.filter((player) =>
        player.title.toLowerCase().includes(this.search.toLowerCase())
      )
    }
  },
  modules: {}
}
</script>

store/index.js

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    players: [
      {
        id: 1,
        names: ['kerem', 'sirin', 'ali', 'ayse', 'ahmet'],
        title: 'Ali',
        pathName: 'ali'
      },
      {
        id: 2,
        title: 'Ayse',
        pathName: 'ayse'
      },
      {
        id: 3,
        title: 'Ahmet',
        pathName: 'ahmet'
      }
    ]
  },
  getters: {},
  mutations: {},
  actions: {},
  modules: {}
})

Answer №1

If you want to make changes to the filteredList, you can do so by:

  computed: {
    ...mapState(['players']),
    filteredList() {
      const filteredPlayers = this.players.filter(player => {
        let flag = false;
        if(player.names) {
         player.names.forEach(name => {
          if(name.toLowerCase().includes(this.search.toLowerCase()) flag = true;
         });
        }
       return flag;
    });
    return filteredPlayers;
  },

To display names, you can use the following structure:

<div class="Players-inner-cards">
          <Clubs class="Players-inner-cards-svg" v-bind:name="players.id" />
          <CostumText tag="strong" lang="tr" class="Players-inner-cards-text">
            {{ players.names.valueOf() }}
          </CostumText>
        </div>

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 can you rearrange the order of objects in an array to only include duplicates?

I don't want to alter the original order of objects in an array. However, I do need to retrieve items in a specific sequence when both the location and place are identical. I attempted a solution but it requires an additional condition. var ...

Ways to remove an item from firebase database

Currently, I am exploring ways to delete data stored in the Firebase database specifically under the requests category. Check out this example Below are the functions I have implemented to fetch and manipulate the data: export default { async contactArtis ...

Converting Variable Values to Element IDs in JavaScript: A Helpful Guide

Is it possible to declare a value in variable A and then access that value as an id in variable B using the method getElementById()? var A = 10; var B = document.getElementById(A); console.log(B); ...

Tips for retrieving return values from AJAX URL content

As I am writing some code to send data from one page to another through the ajax URL, I encountered an issue where the retrieved values on the previous page are showing as null. The first page code looks like this: <script> function myFunction() { ...

Using a global variable in Vue and noticing that it does not update computed variables?

Currently, I'm in the process of developing a web application and have begun implementing authentication using firebase. Successfully setting up the login interface, my next step is to propagate this data throughout the entire app. Not utilizing Vuex ...

Using AngularJS, we can create a nested ng-repeat with an expression to filter the

I'm having trouble using a value from the initial ng-repeat as a filter in the nested ng-repeat. The issue lies with {{alpha.value}}. It displays correctly in the first repeat, including the filter and the h3 tag. However, in the second repeat, it s ...

Laravel encounters an issue when trying to access the user ID

I am currently working on a project using Laravel and Vue. I'm trying to pass the user ID to the table when the user is authenticated, but I'm facing an issue where the authenticated user's ID is not being passed to the query, resulting in t ...

Sort object keys in a Vue component by the English representation of numbers in descending order

I've got some data for a dropdown menu. The list is currently in a random order, but I'd like to arrange it in descending order based on the key values (One, Three, Five, ...). new Vue({ el: '#app', data: { data: { Abc: ...

method that provides route-specific variable outputs

I have a situation where multiple routes require the same userdata from the database. Although I have a function to verify if the user is logged in, this function does not return the user variables. Here is an example route: app.get('/template', ...

Using callbacks in Node.js to pass variables

I'm relatively new to working with node and I'm attempting to develop a function that retrieves server information. However, I've encountered an issue. I've set up a config object (which will eventually be dynamically updated by certain ...

Managing user sessions in Node.js

What is the best way to manage SESSIONS in Node.js? Specifically, I am interested in storing a UserID in a session using Node.js. How can this be accomplished, and is it possible to access that Node.js session in PHP as well? I am looking for an equivale ...

creating reactive images with Vue

The original code utilized an image in a menu as shown below: <img :alt="$t('more')" class="mobile-plus-content visible-xs" src="../../../assets/img/plus79.png" /> This results in: src="data:image/png;base64,the image" I made a mo ...

issues arise when using array_merge, serialize, and unserialize functions in PHP

I am faced with the following task: Insert an associative array into a database field so that it can be reused as an associative array. [Completed using serialize($associativeArray)] Retrieve the associative array from the database and view it as an arra ...

You are unable to use multiple background colors on a material UI snackbar

Is there a way to apply multiple background colors to a material UI snackbar? I attempted using linear-gradient as shown below, but it didn't work. import Snackbar from 'material-ui/Snackbar'; const bodyStyle = { border: `2px solid ${co ...

Bringing in d3js into Angular 2

Is there a way to successfully import d3js into an Angular2 project? I have already installed d3js using npm and added it to my systemJs, but am encountering a traceur.js error. I also attempted to just use the latest cdn in a script tag and tried import * ...

What steps should I take to solve this wheel-related issue in my Vue 3 application?

I have been developing a Single Page Application (SPA) using Vue 3, TypeScript, and The Movie Database (TMDB). Currently, I am tackling the implementation of a search feature and I've encountered a bug. The "search-box" component is located in src&b ...

Failed to build development environment: Unable to assign the attribute 'fileSystem' to a null value

I'm attempting to launch an Ionic 2 Application, but I keep encountering this error when running ionic serve Error - build dev failed: Unable to assign a value to the 'fileSystem' property of object null Here is the complete log: λ ion ...

What methods are available to verify all my (JavaScript) AJAX calls when they are being sent to Node.js?

My web app sends hundreds of HTTP requests to a Node.js API I created. I need to ensure that only authenticated requests are accepted by Node.js. The issue at hand: I don't want to manually update each AJAX request in my JavaScript code to include a ...

The equivalent of ESM for resolving modules using the `createRequire` function with a specified

In the process of developing a JavaScript instrumentation engine, I am currently focused on traversing a source file's Abstract Syntax Tree (AST) and queuing imported modules for instrumentation in a recursive manner. In order to achieve this, it is c ...

Debugging the Force-Directed D3 Graph

I stumbled upon a fantastic article that provided a detailed guide on creating a beautiful D3 force layout graph. However, I'm facing some difficulties with the JSON source: The "links" attribute in the author's JSON doesn't seem clear to m ...