Include solely distinct items in the array

I have a function that creates an object:

$scope.checkPosRole = function(possition, posRole, posFunction) {
   var rolePos = {pos: possition, posRole: posRole, posFunction: posFunction}; 
   $scope.rolePossition.push(rolePos);
   };

The issue I'm facing is that I only want one object with a specific value for pos in the array. If an object with a duplicate pos value is added to the array, I want to replace the existing object with the new one.

I've attempted using various functions like foreach to scan through the array, but haven't been able to achieve the desired outcome. Any assistance would be greatly appreciated.

Answer №1

give this a shot

var positionRoles = [{ id: 1}, {id: 2}, {id: 3}];

function updatePositionRoleInfo(roleData) {
  var posIndex = -1;
  
  for(var j = 0; j < positionRoles.length; j++) {
    if(positionRoles[j].id === roleData.id) {
      posIndex = j;
    }
  }
  
  if(posIndex > -1) {
    positionRoles[posIndex] = roleData;
  } else {
    positionRoles.push(roleData)
  }
}

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

Creating an Image Link within Nivo Slider

I have been trying to figure out how to make an image a link in NivoSlider. I know that I can use captions to create a link, but I want the actual image to be clickable for better accessibility. I found a similar question on StackOverflow, but it was rela ...

data retrieval not refreshing sorting post construction

My challenge involves fetching data from MongoDB with sorting. While it works perfectly on the development server, it fails to update the data after being built and hosted. import Review from "@/models/review"; import connectdb from "@/util/ ...

Issue with date: date.toLocaleTimeString function is invalid

I'm currently working on creating a time display component using hooks, but I'm running into an issue with the error message "TypeError: date.toLocaleTimeString is not a function." As a newcomer to this technology, any guidance would be greatly a ...

Having trouble getting jQuery to function properly in Safari

I am facing an issue with jQuery in the Safari browser. While my jQuery functions are functioning perfectly on GC, FF, IE, and Opera, they seem to fail when tested on Safari. My question is: Is there a specific code snippet that I can integrate into my w ...

Navigating child components within a flat list using React Native

Hello everyone, I am relatively new to React Native and JavaScript, and I'm currently trying to grasp the concept of react native navigation. In my project, I have a home screen that contains a CardFlatList component with multiple CardComponents. The ...

The shiny conditional panel fails to evaluate upon initialization

When utilizing conditional panels to dynamically build up my Shiny app, I've noticed that the condition the panel is based on is not initially evaluated. The panel remains visible by default and only becomes hidden after certain actions are taken. Thi ...

I encountered a console issue that I am struggling with. It is showing the error message "TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'"

When running this code, I encountered an error in the console (TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'). Can someone help me identify where I made a mistake and provide guidance ...

Experiencing issues with connecting to jQuery in an external JavaScript file

My jQuery formatting in the HTML file looks like this: <!doctype html> <html lang="en"> <head> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js" type="text/javascript"></script> </head> < ...

changing the core JavaScript of a keyboard plugin

To access the demo, click on this link: . You can find the main javascript file at https://raw.githubusercontent.com/Mottie/Keyboard/master/js/jquery.keyboard.js. Is there a way to switch the positions of the accept and cancel buttons? ...

Experiencing issues with autoungrabify or autolock in cytoscape.js?

I have been working on a web application using Cytoscape.js, and I recently integrated the Edgehandles extension to allow users to add edges. The two types of edges that can be added are undirected and directed. Adding directed edges is functioning as expe ...

Display infobox within agm-marker-cluster

When trying to open the infoWindow inner agm-marker-cluster, I encountered an error message: Cannot read property 'then' of undefined This error occurred in the following line of code: return _this._markerManager.getNativeMarker(infoWindow.hos ...

Utilize the Power of React.js and Express.js to Send Emails

After building a web app using React.js in ES6, I found myself wanting to add a "Contact Us" page that allows users to send an email. However, as a newcomer to React, I discovered that React itself cannot directly send emails. Following tutorials with node ...

Retrieve the top K elements from a given array

I'm currently exploring the most efficient method to retrieve the top k elements from a given input array. One approach would involve sorting the array and then extracting the k elements from the end of the sorted array. There are alternative metho ...

Uh-oh! An unexpected type error occurred. It seems that the property 'paginator' cannot be set

I am developing a responsive table using Angular Material. To guide me, I found this helpful example here. Here is the progress I have made so far: HTML <mat-form-field> <input matInput (keyup)="applyFilter($event.target.value)" placeholder ...

What is the best way to update an array of string values with corresponding IDs from a different table

data set 1: ID Array 100 ["a","c","b"] 200 ["d","e","c","a"] data set 2: Letter ID a 1 b 2 c 3 d 4 e 5 Desired output: ID ID_Array 100 [1,3,2] 200 [ ...

Utilizing AJAX and PHP to refresh information in the database

For my project, I need to change the data in my database's tinyint column to 1 if a checkbox is selected and 0 if it is deselected. This is the Javascript/Ajax code I have written: <script> function updateDatabaseWithCheckboxValue(chk,address) ...

What is the method to prolong the end date of fullCalendar.js through Javascript?

I am facing the same issue as this individual. I am unsure of how to add one day to the end date. I do not want to alter the database value, only modify it on the HTML page. Currently, this is my calendar (no Moment.js joke intended): $(document).ready(f ...

Activate a function upon the clicking of a button by utilizing a directive in Angular.js

In my directive, there is a function called "myFunction()", and in the template, I have a button. When the button is clicked, I want to execute the function without using ng-click for specific reasons. Instead, I am looking to assign a class to the button ...

Create a specific part of a webpage that can be scrolled through

In a unique way, I have designed a page where the only way to navigate is by simply clicking. To achieve this, I have implemented the following custom CSS: body{ overflow:hidden; } However, I have encountered a problem where I am unable to scroll to ...

What is the best way to navigate back to the previous state in reactjs?

I have implemented a sidebar component with its initial state set to false in order to hide the block until a user interacts with the hamburger icon. Once clicked, the state changes to true and displays the sidebar menu. However, I am facing an issue whe ...