Remove any JSON objects in JavaScript or AngularJS that match another JSON object

JSON A ['711','722','733','744']

JSON B [{pid: 711, name: 'hello'},{pid: 733, name: 'world'}, {pid: 713, name: 'hello'},{pid: 744, name: 'hellosdaf'}]

I am attempting to remove elements from array B that match the values in JSON A. For example, if JSON B contains elements with PID 711, 733, and 744 which are also present in JSON A, I want to delete them.

I have tried using the function below but it is not working as expected, sometimes missing one or two strings during deletion:


angular.forEach(B, function(value, index){
    if(A.indexOf(value.pid) > -1){
        B.splice(index , 1);
    }
});

Answer №1

Iterate over array A and then apply a filter to array B using the filter function, incorporating an iterator function to only return elements where the key "pid" does not match.

for(var element of A){
  B = B.filter(function(item){
    return item.pid !== parseInt(element)
  });
}

I trust this explanation is beneficial.

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

Retrieve the $http data from a function that is external to it

Apologies if the title is confusing, it's hard to explain in a different way. The situation I have is... app.controller('viewProductController', ['$scope', 'dataFactory', '$routeParams', function($scope, dat ...

Are you currently working on a Ruby on Rails application?

Seeking guidance from the StackOverflow community on best practices when taking over a Rails app from another developer. I am stepping into a new role as the lead developer at my workplace. While I have experience in front-end, SQL/Mongo, Node.js, and kno ...

Steps to generate a newline-separated JSON-lines file from a collection of Python dictionaries

To meet the requirements for online prediction on Google Cloud AI platform, I am working on creating a JSON-lines file containing my data. My data is currently stored as a list of dictionaries, structured like this: data = [{'values': [0,1,0], ...

JSON input in react.js sadly ended abruptly

I am encountering a problem when I attempt to submit the form. Unhandled Rejection (SyntaxError): Unexpected end of JSON input Whenever I press the submit button, this error occurs, here is the code snippet: onButtonSubmit = () => { this.setState({ ...

Setting up a custom destination for installing a NuGet package

I recently created an ASP.NET 5 application in Visual Studio Community Edition. I attempted to add the nugget package angularjs.TypeScript.DefinitelyTyped using the command Install-Package angularjs.TypeScript.DefinitelyTyped and also through the NuGet Pac ...

The react-native-video-controls package was used in React Native. When the onBack event was triggered, it successfully navigated back to the initial screen. However, upon returning

https://i.stack.imgur.com/GsS3d.png https://i.stack.imgur.com/HYBOU.png Attached are screenshots showing the issue I am experiencing with my video player. When I click the back button, the screen does not return to its initial state and the flatlist items ...

Struggling to make $http post requests to PHP using Angular

I am experiencing difficulties in transferring data to my PHP file after decoding Json, resulting in an error. I need assistance in identifying where I have made a mistake. app.js $scope.formPost = function(){ $http.post('sendmail.php' ...

Implement Material-UI Higher Order Components in a Class-based Component

I'm in the process of incorporating material UI into my class component rather than converting everything to Hooks. I'm unsure which approach would be simpler, utilizing Hooks or adapting what I currently have. https://material-ui.com/styles/bas ...

What is the best method for converting IDs into objects within ng-options in Angular?

Is there a way to dynamically use an array of IDs as the source of my ng-option directive inside of select? Instead of creating an array of objects with corresponding IDs, I am wondering if there is a method to set a function as the source of ng-option. ...

Creating a tooltip with a left arrow and a bordered design

I am looking to create a tooltip that displays its content after clicking on the tooltip tip icon. Currently, it only works on hover, but I want it to be clickable and mobile responsive. Desktop design should resemble: https://i.sstatic.net/FQPyt.png Mob ...

The steps to display a partial view within another view in React Native

Attempting to show the View after calling alert("Hi") in the renderMoreView function has been challenging for me. The alert works fine, but displaying the View is where I am encountering issues. class Main extends Component { state = { moreButton: f ...

What is the mechanism behind making a Promise appear synchronous when using a Proxy in JavaScript?

const handler: ProxyHandler<any> = { get: (target: Promise<any>, prop: string, receiver: any) => { return target.then((o) => { return o[prop].apply(o); }); }, }; return new Proxy(obj, handler) ...

Mastering the use of JArrays in the copy activity of Azure Data Factory

Currently, I am facing an issue with my copy activity where I am using a MongoDB JSON file as the source data and attempting to sink it into an Azure SQL Database. The problem arises from the fact that my JSON file consists of an array of email addresses. ...

What is the best way to retrieve information from a json file using axios in a React project?

I'm having trouble retrieving JSON data. { ID string `json:"id"` Amount int `json:"amount"` Month string `json:"month"` PayFailed bool `json:"pay_failed"` } I’ve written the foll ...

Display a hidden division when a radio button is selected using Javascript and Jquery

Currently, I am in the process of constructing a form that triggers a series of additional questions based on the selected answer. Below is my current working code snippet: $(".appliedWorked").click(function(){ if($(this).val()==="appliedWorkedYes") ...

Is it possible to incorporate an additional value into the jQuery widget 'Autocomplete' by using a second variable?

For several years, I have been utilizing the jQuery 'Autocomplete Widget' in my projects. This plugin allows me to pass a value labeled as 'term' to the PHP SQL code that works like this: $( "#cs1" ).autocomplete({ aut ...

When using Selenium with Java, interacting with checkboxes produces varying results compared to manually clicking on them

I'm encountering an issue with clicking on a specific checkbox that should be checking all the other checkboxes below it (root parameter). Previously, when using the following code on the element: arguments[0].click(); Attempting to use the regular ...

Utilizing the power of JavaScript/JQuery to showcase a compilation of all input values within an HTML form

I'm struggling to showcase all the input values from my HTML form on the final page before hitting the "submit" button. Unfortunately, I am facing a challenge as the values are not appearing on the page as expected. Despite several attempts, the valu ...

Decoding JSON using abbreviated keys in C#

I am in the process of transferring a project from Android to Windows Phone 8 and am struggling to find information on how to specify which JSON key corresponds to each object field. In my Android code, I utilized Google GSON and the SerializedName annota ...

Contrasting an array of items with an array of Objects

I need to match the values in an array called ingredientName with a corresponding array of objects. Here is how they look: const ingredientName = ['chicken', 'cheese', 'tomato', 'lettuce']; let imageObjects = [ { ...