What is the best way to retrieve and modify an AngularJS object within an array using its unique id value?

Let's say we have an array of objects:

ctrl.items = [Object, Object, Object]

Each object in the array follows this structure:

{id:1, item:"item", quantity:2}

These objects are displayed in the front-end using the following binding:

<div ng-repeat="item in ctrl.items">
    <span ng-bind="item.item"></span>
    <span ng-bind="item.quantity"></span>
</div>

When a specific id is received from the back-end and passed to a function called 'updateItem', like this:

updateItem(id)

The task is to update the object in ctrl.items with a matching id (specifically, increase the object's quantity by 1). After retrieving the relevant item using:

self.item = $filter('filter')(self.items, {id: id}, true);
self.item.quantity = self.item.quantity + 1;

The main question is what steps to take next? (Considering that self.item is currently a variable. How can we replace the item inside ctrl.items with self.item?).

Answer №1

iterating through ctrl.posts to find post with matching ID
  if(post.id matches myId)
     increment the likes count for that post

Answer №2

To efficiently handle data manipulation, consider leveraging lodash along with its useful map function. Detailed information can be found at lodash map function

Answer №3

If you want to utilize only plain javascript, you can follow these steps:

const index = dataList.map(function(element) {return element.key; }).indexOf(myKey);
const foundElement = dataList[index];

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

Enhance dynamically generated HTML using jQuery Mobile

Using jQuery Mobile version 1.2.0 I am dynamically generating HTML using JavaScript ($(selector).html(content)), adding it to the DOM, and then displaying it ($.mobile.changePage()). After that, I make an AJAX call, retrieve some data, and re-generate th ...

Retrieve the chosen date from a DatePicker using a Javascript function

Is there a way to retrieve the user-selected date using a JS function? I attempted this method: var test = $("#datepicker").datepicker( 'getDate' ); However, when I display it with an alert, it shows [object object] instead of the date. This ...

A guide to successfully transferring textarea content to the clipboard in an Angular application

I'm struggling with a task in an Angular 7 application where I have a textarea and need to copy its content to the clipboard when a button is clicked. I've implemented the following code in the button's click handler: if (this.txtConfigFile ...

Guide on how to navigate to the bottom of a div element using Selenium Webdriver

My current project involves a unique challenge where there is a specific div element on the webpage that acts as a popup dialog when a link is clicked (similar to Facebook reaction dialog boxes). To automate tests for this scenario, I am using Selenium We ...

I seem to be stuck on the Pokemon Damage Calculator kata on codewars. I've been trying to pass it, but I

My function seems to be working well, but I'm having trouble passing all the tests. Can anyone offer some assistance? You can find the kata at this link: function calculateDamage(yourType, opponentType, attack, defense) { let key = yourType + opp ...

Deactivate event handling for viewport widths below a specified threshold

Currently, I am attempting to deactivate a script when the width of the window falls below 700px. Despite reviewing suggestions from various sources, I have not been successful so far. window.onresize = function () { if(window.innerWidth < 700) { ...

Make the jQuery toggle() function work like a regular radio button when selecting multiple options at a time

I have recently created two radio buttons using <i> font icons. Previously, I had successfully used the same code to create a checkbox, so I applied it to the radio buttons as well. After fixing the positioning, everything seemed fine when interactin ...

Using Nextjs Image as the Background for Layout

I have a question regarding implementing a common background image for all pages in my app. Currently, I have the main page.js code that displays an image as the background using Next Image component. However, I would like this background to be present thr ...

Handling concurrent requests in DjangoDiscover how Django manages multiple simultaneous requests

Handling multiple requests in a web application can be a challenging task, especially when the server needs to perform complex operations like making API requests and executing database queries. In this post, we will explore how Django can effectively mana ...

Retrieve PDF from Controller using jQuery AJAX request

While searching for solutions on how to create a PDF using a controller in EvoPDF, I noticed that none of the examples addressed the scenario where the controller is called via jQuery AJAX. In my application, I have a simple jQuery function that sends dat ...

Enhance the functionality of the directive form by incorporating additional methods

My Angular custom form needs enhancement: <form rp-form> <input type="text" value="one"> <input type="text" value="two"> <button type="submit">submit</button> </form> I am looking to incorporate a method using th ...

Establish the state as the result of a function

I need to update the state of timeToCountdown with the value stored in allTimeInSeconds. Next, I intend to pass this data as a prop to a component. class Timer extends Component { constructor(props){ super(props); this.state = { ...

Converting Firebase TIMESTAMP values to human-readable date and time

Utilizing Firebase in my chat application, I am adding a timestamp to the chat object using the Firebase.ServerValue.TIMESTAMP method. I want to display the time when the message was received in the chat application using this timestamp. If it is the cur ...

Introducing a fresh parameter to initiate and end Server Sent Events

Assistance needed. I have implemented Server Sent Events to dynamically update a website using data from a database. Now, I aim to send a new parameter ('abc.php/?lastID=xxx') back to the PHP script based on information received in the previous ...

Obtain the identification address for a group of items

I have a JSON object containing place IDs, and I am attempting to retrieve the corresponding addresses for each ID. This is the code snippet I'm using: <div id="places" class="places"></div> <script> function initialize() { j ...

Utilize GetXmlHttpObject for an AJAX request to interact with a PHP script

I am currently attempting to call a PHP file using the GetXmlHttpObject object and so far, I have had some progress. However, it seems that there is an issue with the URL variable. Do I need to handle the URL string in a different way? Here is the releva ...

Updating React state by changing the value of a specific key

I've been diving into React lately and I'm facing an issue with updating state values for a specific key. Below is my current state: this.state = { connections : { facebook : "http://facebook.com", flickr : null, ...

Mobile.changePage does not trigger the pageinit event in jQuery Mobile

I've encountered an issue with handling the on-click event of a button in the following code snippet: $(document).on("click", '.submit_review_button', function(event, ui) { var place = $.data(this, "object"); var ttext = $("#revie ...

Troubleshooting issues with document.querySelectorAll in JavaScript

Can someone explain why document.querySelectorAll isn't functioning properly in this code snippet? I'm a beginner and could use some help. <p id="demo">This is a paragraph.</p> <h1 id="1demo"> Hello </h1&g ...

The jQuery form validation feature surprisingly doesn't take into account the value of the submit button

I have implemented jQuery form validation from Here is an example from demo.html <form id="form-signup_v1" name="form-signup_v1" method="get" class="validation-form-container"> <div class="field"> <label for="signup_v1-username">Fir ...