Linking a user's input to a specific element within an array

Can someone assist me in displaying the contents of an array based on a specific id input? I attempted to use v-model but couldn't figure out a solution. For instance, when I input 123, I want to see item1 displayed and when I input 321, I expect to see item2.

<template>
    <input v-model="inventar[1].id" />
</template>

<script lang="ts">
    import { defineComponent, reactive } from 'vue'

    interface Inventar {
        id: number,
        name: string,
        size: number
    }

    export default defineComponent({
        name: 'HelloWorld',
        setup() {
            const selected: number = 123;
            const inventar: Array<Inventar> = [];

            inventar.push({ id: 123, name: "item1", size: 25 } as Inventar);
            inventar.push({ id: 321, name: "item2", size: 20 } as Inventar);

            return {inventar,selected}
        }
    })
</script>

I would greatly appreciate any assistance. Thank you!

Answer №1

Here is a simple implementation that you can use as a guide for applying to Vue 3 and TypeScript. Check out the codepen example below:

<template>
  <div id="#app">
    Object: {{ found }}
    <div>
      <input type="text" v-model="search" />
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      inventars: [
        { id: "123", value: "123" },
        { id: "321", value: "321" }
      ],
      search: ""
    };
  },

  computed: {
    found() {
      return this.inventars.find((val) => val.id === this.search);
    }
  }
};

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 on expanding typings in TypeScript?

In my software library, there exists a map function with the following definitions: function map<T, U>(f: (x: T) => U, a: Array<T>): Array<U> function map<T, U>(f: (x: T) => U, a: Functor<T>): Functor<U> Furtherm ...

How can I access other properties of the createMuiTheme function within the theme.ts file in Material UI?

When including a theme in the filename.style.ts file like this: import theme from 'common/theme'; I can access various properties, such as: theme.breakpoints.down('md') I am attempting to reference the same property within the theme ...

Restrictive discriminated union via function argument

I am in possession of a shop that organizes a variety of types based on their IDs interface Dog { type: "dog"; woofs: string; } interface Cat { type: "cat"; meows: string; } type Pet = Dog | Cat; type AnimalState = Record<string, Pet ...

Identify when the user ceases typing in Angular 2

I am currently working on implementing a feature that detects whether the user is typing or not. I need to determine when the user has stopped typing for at least 3 seconds in order to perform certain actions. I have successfully detected when the user sta ...

Dealing with TS error - The target demands 2 elements, while the source might contain fewer items

As a newcomer to TS, I'm struggling to grasp why TS believes that Object.values(keyCodeToAxis[keyCode]) could potentially result in an array with less than 2 elements. type ControlKey = "KeyQ" | "KeyW" | "KeyE" | "Ke ...

Exploring the power of Vuejs3 with Internationalization and the Composition API

Currently, I am working on a frontend interface in VueJS and integrating i18n with Vuejs 3. While the normal implementation of i18n works fine, I encountered issues when trying to use it with the composition API. In my main.js file, I have set up i18n as ...

Utilizing custom types in React with TypeScript and Prop-Types

Currently, I am working with a string type literal that looks like this: type MyType = 'str1' | 'str2' | 'str3'. I need one of my props to only accept this specific type, but I'm struggling to specify this in PropTypes. ...

What is the best way to showcase a rating value in vue.js?

Here is my JSON data: {"status": true, "reviews": [{"review": "scdbgnhvgdbsr", "rating": 5, "by": "aravind", "pid": 8, "review_id": 1}, {"review": "helsocxdxvfbgfnhfgdf", "rating": 2, "by": "ab", "pid": 8, "review_id": 2}], "rating": 3.5} I have successf ...

Is there a way to load an image onto the ngx-image-cropper without triggering the imageChangedEvent event?

My latest project involved creating a custom cropper using ngx-image-cropper, which allows for cropping and rotating images. For the sprint demo, I needed the images to be displayed as soon as the application loads without having to trigger the fileChangeE ...

Setting up a beautiful flickity carousel on vuejs and nuxtjs: A step-by-step guide

As a budding vuejs developer, I have been exploring vuejs for some time now and I am ready to dive into a project using this technology. Recently, I came across nuxtjs which offers server-side rendering capabilities. Excitedly, I integrated Bootstrap4 int ...

Issue with Vue.js input not updating with v-model after input sanitization in watch handler

Recently, while working with Vue 2.6, I came across an unusual issue when trying to sanitize user input. The main culprit seemed to be a custom component that housed the input field. Here's a simplified version of it: <template> <input :na ...

Converting a string URL to an object type in TypeScript

Is there a way to convert a string URL into an object type in TypeScript? Here is some sample code: type KeyUrl<T> = T extends `/${infer U}` ? U : never; type TUrl<T> = { [k in KeyUrl<T>]: string }; // ---------------------------------- ...

How can I resolve the ReferenceError in NextJs which states that Audio is not defined?

Recently, I implemented a Next component that acts as a music player, allowing users to play or stop the audio just like an MP3 player. While the functionality works perfectly fine on my local server – clicking the button triggers the audio play or pause ...

delayed updating of property not visible in angular 10 immediately

I needed to hide a div based on a condition, so I decided to use the hidden property like below: <div [hidden]="isControlDisplayed()?false:true"> The isControlDisplayed() method determines whether to show or hide the div based on the value ...

Tips for preventing errors when deploying a next.js app to production and encountering type<any> errors caused by typescript

Encountering an error during the application build process related to the any type. Specifically happening in the building process of a next.js app. Tried a solution but still facing issues while building the app, receiving errors associated with the any ...

Tips for verifying the variable type in a TypeScript ESLint custom rule

Trying my hand at creating a custom TypeScript-eslint rule that requires the callee's object of a node to be of a specific type, which I'll refer to as MyType. Utilizing the CallExpression from typescript-eslint in the code snippet below: Source ...

Manipulating variables across various methods in TypeScript

I have a simple code snippet where two variables are defined in the Main method and I need to access them from another method. However, I am encountering an issue with 'variables are not defined', even though I specified them in the declerations ...

Issue with toggling in react js on mobile devices

Currently, I am working on making my design responsive. My approach involves displaying a basket when the div style is set to "block", and hiding it when the user browses on a mobile device by setting the display to "none". The user can then click on a but ...

Angular 4: The parameter 'typeof CLASS' cannot be assigned to the parameter 'INTERFACE'

Exploring dynamic components in Angular has been quite interesting for me. However, I've encountered a roadblock that I'm unsure how to overcome. This is the interface I am working with: export interface InjectableComponent{ setData(data: a ...

Creating a column that adjusts dynamically in PDFMAKE

Currently, I am utilizing PdfMake within my VueJS app to generate PDFs. I am curious as to whether it is possible for me to have control over the columns displayed in my template by using the data that is being printed as a variable. I am aiming to achiev ...