Eliminate item from array if it is found in JSON data

I have implemented a script to eliminate JSON objects from an array that exist in both the array itself and another JSON object:

var stageChildren = stage.sprites;
for (var i = 0; i < stageChildren.length; i++) {
    for (var x in mainMenu) {
        if (mainMenu[x] === stageChildren[i]) {
            console.log(x);
        }
    }
}

To clarify, let's consider two objects named: object1 & object2.

In object1, there may be a common JSON object present in object2 as well. In such cases, the object is removed from object1.

Although this script functions correctly, I suspect it could significantly impact performance. Why? Because there are approximately 50 distinct objects within stageChildren, and 10 inside mainMenu. The script iterates through each object in stageChildren, checks if that object also exists within mainMenu (through another for loop), and proceeds to the next 49 objects.

Is there a more efficient method to achieve this?

Answer №1

let currentIndex = 0;
let stageElements = stage.sprites;
for (let item in mainMenu) {
    if (stageElements.includes(mainMenu[item])) {
        const foundItem = stageElements.includes(mainMenu[item]);
        let indexToRemove = stageElements.indexOf(foundItem);
        stageElements.splice(indexToRemove, 1);
    }
}

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

Develop a Java program that generates a two-dimensional array containing HashMap objects

I am interested in creating a 3-dimensional array in Java with the first two dimensions ranging from 0 to 100 and 0 to 4, while the third dimension consists of dictionary indexes such as 12, 18, 22, 49, and so on. Could someone guide me on how to set this ...

Converting byte arrays into character arrays using Java

I have written a basic code snippet for converting between byte arrays and char arrays. While there are many examples available online, I find this method to be the simplest and most straightforward. It is important for me to use arrays instead of strings ...

Only display the parent component in React if there are children components that will be rendered

Navigating this situation in React has me puzzled. Within a group of tabs, I have several chart components that render based on their permission prop. If the user lacks access to a chart, it won't be displayed - straightforward enough. However, I&ap ...

Converting a list of data into a two-dimensional array for easy processing

After reading a file into a C# console app, I have a dataset that includes player information like this: Player;Team;POS;HR;RBI;AVG Abreu, J;CWS;1B;30;101;0.29 Altuve, J;HOU;2B;15;66;0.313 Andrus, E;TEX;SS;7;62;0.258 Now, I need to sort all 143 ite ...

Utilize Vuex mutators within route navigation guards

Hey there, I'm currently working on an app using Laravel and VueJS. To restrict certain routes, I've implemented navigation guards. However, I'm facing an issue where I need to access Vuex mutators to determine if the current user is logged ...

Error message: "An issue occurred in Android where a java.lang.String value in the result cannot be converted to a JSONArray."

I followed the instructions in this link to implement JSON RPC. The response I receive is as expected, but I encounter a JSON error when attempting to parse it. This is my code: JSONEntity entity = new JSONEntity(jsonRequest); HttpPost request = new ...

Trouble with Loading Google Place Autocomplete API in HTML

I utilized the Google MAP Place Autocomplete API for a web project. Click here to view the code and output. <form> <input id="origin-input" type="text" class="form-control" placeholder="From"/> <br/> <input id="de ...

The ThreeJS scene may run smoothly at 60 frames per second, but it maxes out my fans and crashes after

Recently, I delved into the world of threeJS and decided to test my skills by using this example: After exporting some terrain from blender and replacing the platform.json file, my scene was running smoothly at 55-60fps. However, as time went on, my compu ...

Issues with data loading from server persist with JSON in JQuery Datatables on IE7, IE8, and IE9

I have successfully implemented Jquery Datatables on a client's admin panel. After testing it on Chrome, Firefox, and Opera, everything seemed to be working perfectly. However, when I attempted to load it on IE7, IE8, or IE9, the system just stuck on ...

Encountering issues with running NPM on my Ubuntu server hosted on Digital Ocean

After successfully installing node (nodejs), I encountered a persistent error when attempting to use NPM. Despite researching the issue extensively and trying different solutions, I have been unable to resolve it. Here is the error message displayed in th ...

The Express.js server encounters a 404 error, causing it to panic and throw an exception before I can even address the issue

(Just so you know, I'm utilizing the Blizzard.js npm package in combination with Express.) I am currently working on a web application that allows users to search for statistics of specific video game characters (profiles created by players). Given t ...

In JavaScript, the array is being passed as "undefined"

<html> <head> <script> function replaceLocation() { var locationArray= new Array(); locationArray[1,2,3,4]=document.getElementById("From").value.split(" "); document.getElementById("map").src="https://maps.google.com/maps?f=d& ...

There was an error while parsing JSON on line 1, where it was expecting either a '{' or '[' to start the input

I've been struggling to extract data from my PHP array that is encoded using json_encode. I'm new to JQuery and have been attempting this for a few days without success. This code snippet is not producing the desired results: $.post('coord ...

Unraveling a stringified string in JAVA

I have a string that looks like this: {"version":"T2"} I retrieved this string from a webview and stored it in a variable called s. I am trying to extract the value of the 'version' key. Here is my code: try { s = s.replaceAll(" ...

Is it possible to add a computed value to the bar's end in Chart.JS?

I'm creating a bar graph that has two bars representing different weeks. Currently, it looks like this: https://i.stack.imgur.com/6Avni.png However, I want it to look like this: https://i.stack.imgur.com/TAVqe.png In order to achieve the desired r ...

Cannot adjust dimensions of Chart.js

Struggling to resize chart.js on an HTML page. Have attempted various methods like adjusting the canvas parameters, defining section and div tags, explicitly setting height and width in <style>, as well as using {responsive: true, maintainAspectRatio ...

Are there any guidelines or rules outlining what is still considered valid JSONP?

I am looking for information on how to properly parse JSONP messages in .NET and extract the JSON data they contain. Is there a current specification that outlines what constitutes a valid JSONP message? During my research, I came across a blog post from ...

Upload the image data into the input file tag

Is there a way to take a string code stored in a JavaScript variable that is retrieved from the Cropit plugin (used for cropping images) and then set this information into an input file tag? I want to be able to send it via AJAX to PHP. Here is the JavaSc ...

Navigating with Angular 2: Expressing HTML 5 Routing Challenges

I'm currently working on a simple web application using Express 4 and Angular 2. The only Angular 2 specific aspect in this project is the utilization of its HTML5 router. Let me walk you through how the routing works in this app: There are two prim ...

What is the best way to transmit data from a single input to two separate APIs within a Next.js environment?

I am working on a registration page that includes an option for users to sign up for a newsletter by checking a box. The code below is what I currently have to handle the form submission: const handleSubmit = async e => { e.preventDefault() cons ...