The Vue.js array matching method

I am attempting to populate my form with the corresponding values from 'myproduct' where the id_product matches the input. However, when I run the code, the value is not returned. Can anyone spot what's wrong with my code?

this.products.forEach(i => {
       if(this.products[i].id == item.id_product)
        {
            
            this.form.product_name = this.products[i].product_name;
            
            this.form.id_category = this.products[i].id_category;
            this.form.description = this.products[i].description;
            this.form.price = this.products[i].price;
            this.form.color = this.products[i].color;
            this.form.size = this.products[i].size;
            this.form.stock = this.products[i].stock;
            this.form.weight = this.products[i].weight;
        } 
    });

Answer №1

In the context of Array.forEach(), remember that the first parameter in the callback function represents the current value, not the index. The second parameter is optional and refers to the index. You can either use just the current value like .forEach(item => {...}) or include the index like .forEach((item, i) => {...})

this.items.forEach(item => {
       if(item.id == product.id)
        {
            this.data.name = item.name;
            this.data.category = item.category;
            this.data.description = item.description;
            this.data.price = item.price;
            this.data.color = item.color;
            this.data.size = item.size;
            this.data.stock = item.stock;
            this.data.weight = item.weight;
        } 
    });

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 how to conditionally render a button based on the presence of a link from a separate file in React

I've been working on my personal portfolio site using React and Tailwind. One challenge I'm facing is how to display the "GitHub" button for each project card only when a GitHub link is provided in my data.js file. Currently, I'm utilizing ...

Using Three.js to give distinct colors to individual vertices within a geometric shape

I am looking to implement picking functionality using IdMapping in Three.js Due to concerns about performance, I have a single large geometry that is calculated as follows: for (var i = 0; i < numberOfVertices; i += 9) { p1 = new THREE.Vector3(grap ...

Adding a JavaScript file to XHTML using XElement

I'm facing an issue when trying to include a JavaScript file from resources into my generated XHTML file. Typically, I would use the following method: new XElement("SCRIPT", new XAttribute("language", "javascript"), new XAttribute("type", "text/javas ...

How to retrieve the content of <p> elements by their id or class in PHP

I am currently utilizing a Script that enables the display of URL content within the meta tag "description". This script utilizes PHP in the following manner: $tags = get_meta_tags($url); Subsequently, it is called like so: <label class="desc"> ...

Tips for incorporating JSON data into an HTML table

https://i.sstatic.net/WEdge.jpgIn an attempt to showcase JSON data in an HTML table, I want the schoolClassName value to be displayed as a header (th) and the first names of students belonging to that specific schoolClass to appear in a column beneath the ...

Is there a way to dynamically change the height in jQuery/JavaScript?

I am encountering an issue with my embed code where the height changes randomly after a few seconds, depending on certain parameters. Here is an example of the code I am using: $( <embed></embed>) .attr("src", "http://www.bbs.com") ...

Is it possible to invoke the unnamed function in jQuery using the .call() method?

Imagine I have a button with the ID #click, And let's say I attach the click event like this: $('#click').click(function(){ alert('own you'+'whatever'+$(this).attr('href')); }); However, I wish to change w ...

PHP issue with array_merge function

Currently working on a PHP project. I have two arrays, array 1 and array 2: $array_1 = array( (more values) 'propub_cost_max' => 5, 'propub_cost_min' => 0.5, 'average_calc_last' => '-1 Months', 'propub ...

Encountering issues with Vue-Router functionality after refreshing page in Laravel-6

Can someone explain what is going on here? Whenever I refresh my web browser, it no longer recognizes the route. ...

What is the purpose of $ and # in the following code snippet: $('#<%= txtFirstName.ClientID%>')

$('#<%= txtFirstName.ClientID%>').show(); Attempting to pass the ClientId as a parameter from server tags to an external JavaScript file. <input type="text" ID="txtFirstName" runat="server" maxlength="50" class="Def ...

Bootstrap import issue: Unexpected token error encountered while test-utils is functioning in one test but not in another

Currently, I am embarking on my unit testing journey with jest in vue. To enable unit tests, I incorporated the plugin from this link: https://www.npmjs.com/package/@vue/cli-plugin-unit-jest This process led to the creation of a test folder housing anoth ...

Redirect events in Backbone views

Is there a way to navigate to a different page when a specific event is triggered in a View using Backbone? events: { 'click .btn': 'signin' }, render: function() { tmp = this.template(); this.$el.html(tmp); }, signin: func ...

Rails backend is struggling to receive Crossrider ajax post requests with JSON payload

I am encountering an issue where my attempts to post a JSON object to a remote server (Rails) are failing. The POST parameters seem to be converted to a url-encoded string instead of being sent as 'application/json'. Here is an example of what I ...

Tips for successfully transferring an image through an XMLHttpRequest

I found this helpful resource at: I decided to test out the second block of code. When I made changes in the handleForm function, it looked like this: function handleForm(e) { e.preventDefault(); var data = new FormData(); f ...

The char array appears to be non-empty, when it should actually be empty

I'm on a quest to locate the prefix shared between two words, but it appears that my current method is flawed. Initially, if(strlen(root) == 0) consistently results in 0. But why? When comparing "astrophysics" and "math," the longest common prefix ...

Fetching json data from the request in Node.js

I'm currently working on a project that involves using the request module to send an HTTP GET request to a specific URL in order to receive a JSON response. However, I've run into an issue where my function is not properly returning the body of ...

Leveraging icons with Bootstrap 4.5

I am currently exploring how to incorporate Bootstrap 4.5 icons using CSS. Do you have any examples of code that could guide me on how to achieve this? I am specifically interested in understanding the required CSS declarations that would allow me to use t ...

Suggestions for VS Code Vue <template>, <script>, and <style> sections auto-complete

It has been a while since I last worked with Vue programming. Recently, when I typed vuedef in a new .vue file, I was pleasantly surprised to see the following code auto-suggested instead of having to manually type it out: <template> </template> ...

Check to see if the specified value is present within the array of embedded documents for the user

Given the User's ID, I aim to determine if they have a groceryList item with a matching name value of "foo". Currently, my query is returning results even when the name doesn't match, likely due to the existence of other values. How can I modify ...

Issues with passing Angular directive attributes to the scope were encountered

I am having an issue with my angular directives where the arguments are not being passed into the scope: app.directive('sectionLeft', function() { return { restrict:'E', scope: { sectionContent: '=', s ...