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

Tips for managing numerous HTTP requests in Angular 6

I have a method that is trying to chain together 3 requests like this: showProfileDetails() { this.getUserInfo(this.currentUser.id).pipe( mergeMap(e => this.getAccounts(this.currentUser.id) ), mergeMap(e => this.getPayments ...

Tips for adding a bounding box to an image received from the server

I've got a python server that is returning a collection of bounding boxes post OCR processing (using Tesseract or similar). "bbox": [{ "x1": 223, "y1": 426, "x2": 1550, &q ...

Personalized text field in date selector using Material UI

Hey there, I've been incorporating Material UI into my react project, specifically utilizing their recommended Material UI Pickers for a date picker. In order to maintain consistency with the rest of my form fields, I'm attempting to customize th ...

Performing a series of synchronous JavaScript calls in Node.js by executing GET requests within a for loop

I'm utilizing super-agent in node to interact with a public API and I'm curious if there's a way to make synchronous calls using it. Although I appreciate its automatic caching feature, my research online suggests that synchronous calls are ...

Using Typescript to import an npm package that lacks a definition file

I am facing an issue with an npm package (@salesforce/canvas-js-sdk) as it doesn't come with a Typescript definition file. Since I am using React, I have been using the "import from" syntax to bring in dependencies. Visual Studio is not happy about th ...

Error: JQuery Ajax Success Handler cannot locate class method

There is a javascript Class in my code that successfully posts data, but encounters an issue when trying to access a specific function within a success handler. Although the function is found during the construction of the class and can be called from othe ...

Adding a class to radio buttons and checkboxes in Angular when they are checked or selected without needing to trigger a change event

I am looking to implement ngClass based on whether an item is checked or not. Essentially, I want the user to visually see which items are selected through radio buttons or check-boxes by adding a class to them, allowing me to apply different CSS styles to ...

The JQuery chosen dropdown experiences a visual issue when placed inside a scrollbar, appearing to be "cut

Good day, I've been using the jQuery "chosen" plugin for a dropdown menu, but I encountered an issue with it being located in a scrollable area. The problem is that the dropdown items are getting cut off by the outer div element. I have provided a si ...

Guide to deploying a Rails app with Vue on the Heroku free plan

My project consists of a front-end built on Vue.js and a backend developed using Ruby on Rails. Now, I am looking to deploy it on Heroku's Free plan. Can someone provide me with the necessary reference documents or helpful links for guidance? Thank yo ...

Tips for Interacting with an API using curl -X Command in Vue

My dilemma is that I have stored wishlist items in a cookie, but when using an incognito tab, the cookie isn't available and the data can't be fetched. I've found a solution in the form of this extension that provides 3 endpoints: https:// ...

What is the best way to link options from a select directive with a different array?

Update: the working jsfiddle can be found here: http://jsfiddle.net/robertyoung/jwTU2/9/ I'm in the process of developing a webpage/app using AngularJS. The specific functionality I aim to achieve involves allowing users to add a row to the timecard ...

Enhance your vuetify pagination with custom features using slots and templates

I've recently implemented the VueJS framework with Vuetify and I'm in need of a more advanced pagination feature than what is currently available. Specifically, I am looking for: An option to customize names (not just numbers) A tooltip displa ...

The JavaScript function Date().timeIntervalSince1970 allows you to retrieve the time

For my React Native app, I currently set the date like this: new Date().getTime() For my Swift App, I use: Date().timeIntervalSince1970 Is there a JavaScript equivalent to Date().timeIntervalSince1970, or vice versa (as the data is stored in Firebase clo ...

What is the best method for setting up message scheduling for a Microsoft Teams bot based on different time

I am trying to figure out how to send messages to users from my bot at specific time intervals. Currently, I'm using agenda for scheduling messages, but I've run into an issue with the timezone discrepancy - agenda is 5:30 hours behind my timezon ...

Unwrapping React: Mastering the Art of Prop Extraction and Sharing

Imagine I have 2 input elements below, both with the same props except for the className <ReactCardFlip> <input type="text" maxLength={1} className={inputStyles.input} defaultValue={value} ...

How can one create a constant in VueJs that can be easily accessed throughout the entire application?

In my Vue.js application, I am able to create a constant through a store, but I have reservations about this practice. Can anyone suggest alternative methods to achieve the same outcome? ...

Error occurred while executing 'npm start' command in ReactJS due to the module 'babel-code-frame' being unable to be located

Every time I attempt to run 'npm start' in the frontend of my application, the terminal spits out a massive error. My package.json file doesn't show any script errors, and I've deleted the 'node_modules' folder and reinstalle ...

Tips for efficiently exporting and handling data from a customizable table

I recently discovered an editable table feature on https://codepen.io/ashblue/pen/mCtuA While the editable table works perfectly for me, I have encountered a challenge when cloning the table and exporting its data. Below is the code snippet: // JavaScr ...

PHP's `json_encode` is failing to properly convert an array and is outputting `{

My system is running CentOS 7.4 with PHP 5.4 installed. $s='a:91:{s:13:"spotsviewvars";s:7:"1916.74";s:13:"100000T18vars";N;s:17:"100000T18S106vars";s:7:"1746.95";s:17:"100000T18S107vars";s:4:"4.49";s:17:"100000T18S108vars";s:4:"8.29";s:17:"100000T18 ...

Auto-complete feature not populating the input field in Google Chrome

Within my register form, I have various INPUT tags present. One of these INPUTs has the name email. <input type=text name=email id=email> When filling out this form in Chrome, I encounter a peculiar behavior. Upon clicking on the email input field ...