What is the best way to retrieve the index of an object within an array?

https://i.sstatic.net/We8lJ.pngHow can I retrieve the index of an object's array? https://i.sstatic.net/hmDKx.png

I am looking to access the index of the items array within the form object.

https://i.sstatic.net/OxePH.png

When adding a new item row, the unit_price of the new row overrides previously created rows.

The issue stems from this scenario

 productSelected(product) {
     this.form.items['id'] = product.id;
     this.form.items['unit_price'] = product.price;
   }

I aim to set the product price to form.items[index].unit_price

Answer №1

It seems like your question is a bit unclear, but based on my interpretation, I will provide two types of answers:

1) Retrieve all the values from the items array

form.items.map((item)=> console.log(item));

2) Retrieve a specific index value:

const findIndex = 0;
form.items[findIndex];

Answer №2

It seems like you are looking to retrieve the index of an item from an array. Here is a solution that might assist you:

myArray.forEach((element, idx) => {
 console.log('element:', element)
 console.log('index:', idx) // index of the element
})

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

The primary objective is to ensure that the input remains contained within the code

I am having issues with my PhoneGap 2.2.0 and Android 4.0.4 application. Whenever I click the input, a div opens above it. Here is a snippet of My Input and My Div: <input type="text" onclick="OpenDiv();" id="MyInput" /> <div id="MyDiv"& ...

Can you provide a basic illustration of how routes are implemented in AngularJS?

After searching through numerous examples of using Routes with Angular, I have unfortunately not been able to find a working solution. Even the example provided in the official guide did not work properly (clicking on it resulted in a wrong URL that did no ...

What is the process for moving an ISO date from one system to

Need help with converting ISO date format? Error: Time value is not valid updateDate = e => { let inputDate = e.target.value const parts = inputDate.split('/') const rearranged = [parts[1], parts[0], parts[2]] const newDate ...

If a user refreshes too quickly or excessively, my server tends to crash

I'm feeling lost and struggling to find answers even through Google search. This is my first solo project where I am developing a MERN full-stack app. Initially, someone warned me it was too ambitious (they were right) and that I would get overwhelme ...

Issue [ERR_MODULE_NOT_FOUND]: The module 'buildapp' could not be located within buildserver.js

I am currently working on a node project using typescript. The project's structure is organized in the following way: --src |-- server.ts |-- app.ts --build |-- server.js |-- app.js In server.ts: import { app } from &q ...

Plot the highest-ranked outcomes based on a search using Google Maps

I'm currently facing a challenge in integrating different components. I have successfully created a php page to display results from a mysql database and can also retrieve addresses for mapping with Google. However, I am struggling to combine both fun ...

Using Javascript and Flask to ensure that at least one checkbox is selected from each category

I have a Flask template that uses a modal window to display a form. The form includes 3 drop-downs that work well with the required attribute, as well as two sets of checkboxes where the user must select one option from each set. I am new to JavaScript and ...

Uncertain about the type of data to send back for Ajax requests in Django?

Looking to improve the functionality of my like button by making it asynchronous using JavaScript. Currently, when I like a post and refresh the page, the like count increases. However, I want the like count to increase without refreshing. To achieve thi ...

Tips for transferring data to a child component's @Input using Observables

I've created an angular component that serves as a tab within a for loop on my HTML page: ... <ng-container *ngFor="let tabData of data$ | async;"> <tab-component id="{{ tabData.id }}" name="{{ tabData.name }} ...

Is it possible to make changes to dynamically inserted HTML using jQuery.ajax?

I'm facing a scenario in jQuery where I make an ajax call that inserts HTML into my website. However, I also need to perform operations on this inserted HTML within the same ajax call's success callback function. Here is a simplified version of ...

Dependency injection in Angular is a powerful design pattern that

I recently completed an Angular 2 website, but I've run into some issues that I cannot seem to debug. I've spent the past week trying to find a solution but have had no luck. Below, I've included some snippets of my code. Feel free to ask fo ...

Prevent typing in text box when drawer is activated by pressing a button

update 1 : After removing unnecessary files, I need assistance with https://codesandbox.io/s/0pk0z5prqn I am attempting to disable a textbox. When clicking the advanced sports search button, a drawer opens where I want to display a textbox. The toggleDra ...

Is there a way to verify if the current event marks the conclusion of the events transmitted to a webhook?

Currently, I am in the process of coding a Google Apps Script web app that will react to events from the Telegram API using a chat bot. The first step was setting up a webhook to collect these events through the Google webhook. Since logger.log is unavaila ...

The onFocus event ceases to trigger once the modal popup appears

One issue that I am facing is with an onfocus event handler. Initially, everything works perfectly after the page loads. However, when I click on a link that triggers a modal popup, the onfocus event stops working. Although focus continues to work as expec ...

Unexpected Error: The axiosCookieJarSupport function is throwing a TypeError, functioning properly in Node.JS but not in .vue pages. What could

Struggling with a function that authenticates on a website, it runs smoothly in a basic node.js script but fails when executed from a .vue page within the NuxtJS framework. The error message received when running in the .vue page is TypeError: axiosCookie ...

Tips for identifying unique digits within numbers

I've encountered an issue with my function that splits the input value on space and then loops through to search for numbers. However, only the last value is shown as checked, not the ones before it. One solution I found was to remove the else statem ...

Issues with Node AssertionErrors cause failures to be silent and prevent proper error output

I am facing an issue with a particular method in my code. The code snippet is as follows: console.log('Trouble spot here') assert(false) console.log('Will this show up?') Upon running this code within my application, the followi ...

What is the best method for ensuring that cheese rises to the top?

Is there a way to increase the value of the variable cheese? I suspect it has something to do with how the variable cheese is defined each time the JavaScript is activated, but I'm not sure how to go about it. Can you offer some guidance on this? & ...

Choose characteristics based on attributes, where the value after the colons is undefined

Hey everyone, I've been exploring ways to select HTML elements like: <div attr:something="else">Random content</div> I discovered that you can target these by using: document.querySelectorAll('[attr\\3A something]') ...

Using Vue.js in combination with Express

Can you assist me in integrating Vue JS with Node JS Express? Hello, I am looking for guidance on how to successfully connect Vue.js with a Node.js Express server. I would like my Node.js server to share data with my Vue.js website, or vice versa where t ...