JavaScript array containing objects remains static

I'm facing an issue with a complex array of objects (I will only display the necessary nodes to explain the problem):

var arr = [
    {
        json: {
              doc: {
                   id: 1,
                   atualizacao:{
                               dc: false
                   }
              }
        }
    }
]

angular.forEach(arr, function(value, key) {
    if(value.json.doc.id == 1){
       value.json.doc.atualizacao.dc = true;
    }
});

Even though I use forEach to change the value of the 'dc' node at a specific position in the array, the array arr remains unchanged at the end of the loop.

Upon further investigation with console logs, I discovered the following:

  • Logging the variable: value.json.doc.atualizacao.dc - IT SHOWS 'true'

  • Logging the variable: value.json.doc.atualizacao - IT SHOWS 'dc = true'

  • Logging the variable: value.json.doc - IT SHOWS 'atualizacao.dc = true'

  • Logging the variable: value.json - IT SHOWS 'doc.atualizacao.dc = false'

  • Logging the variable: value - IT SHOWS 'json.doc.atualizacao.dc = false'

Answer №1

Things are evolving, check out this link: Plunker

When using angular.forEach, you only need to pass the value for the function.

Feel free to explore the provided Plunker for more insights.

Answer №2

It seems like there may be an issue with the $rootScope that I'm starting to consider. The object in question is actually quite intricate, extensive, and substantial compared to what I initially shared. After altering its value to true, it appears that my change is still in effect at the conclusion of the forEach loop. This could explain why a partial log of the object displays true, while a log of the root object shows false. Does this explanation hold any validity?

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

What is the most effective way to reset the state array of objects in React JS?

I'm facing a challenge in resetting an array of objects without having to rewrite the initial state value again. Initial state const [state, setState] = useState([ {name: 'abc', age: 24}, {name: 'xyz', age: 20} ]) Is there a metho ...

Hide the menu by clicking anywhere outside the React component

I'm working on a menu component that needs to close when clicking anywhere on the page while open. Is there a method to achieve this without having to add an event listener to the document and check the event.target? Unfortunately, I cannot send the ...

Creating HTML tabs using jQuery with input[type="radio"] tutorial for beginners

I'm having trouble creating a tab with input[type="radio"] using Jquery. The tab works fine on the first click, but it doesn't work on the second click. I can't figure out where the issue is in the Jquery code. Can someone assist m ...

Is there a way for me to extract and showcase the initial 10 items bearing a particular class name from a different html document on my homepage?

I am looking to extract a list of movies from an HTML file titled "movies.html". The structure of the file is as follows: <div class="movie">Content 1</div> <div class="movie">Content 2</div> <div class=" ...

Guide on parsing JSON data received from the frontend

Here is the HTML code that I am working with: <div id="loginform"> <form class="loginIn" name="loginform"> <input type="text" name="login"> <input type="password" name="password"> <input type="submit" value="Войт ...

exploring alternatives to ng-container in angular-4.x for selecting elements

Currently in my Angular 4.x project, I have a component using the Selector 'abc' as shown below: @Component({ selector: "Abc", templateUrl: "Abc.html", styleUrls: [ "Abc.css" ] }) However, the "Abc" tag is also present in the DOM, b ...

The jQuery Cookie is failing to be set with every click as expected

I am currently working on a layout switcher feature. When a user clicks on a specific div with the class name display, it triggers the toggling of another class called display-grid for visual enhancements. This functionality also involves switching classe ...

A step-by-step guide on utilizing the reduce function to calculate the overall count of a user's GitHub repositories

I need help finding the total number of repositories for a specific user. Take a look at my code below: Javascript const url = "https://api.github.com/users/oyerohabib/repos?per_page=50"; const fetchRepos = async () => { response = await f ...

What is the process for sending a JSON response and then redirecting to an HTML page after a successful event in Node.js using Express?

Trying to send a JSON response and redirect the page simultaneously in Express.js. Need help figuring out how to achieve this. Is it possible to redirect in Express.js while sending a JSON response to the client? The goal is to use this JSON data to render ...

Using a for loop to populate a 2D array by combining two 1D arrays

To create a two-dimensional array from two one-dimensional arrays, the first row should consist of data from arrayCentri while all the other rows should be filled with data from arrayTocke. The number of columns should be equal to the number of elements in ...

Is there a way to guide the user to a different page while still inside a getJSON function?

After making an external call to a JSON file, I am facing an issue where if the JSON data is null (possibly due to expired user session), the user should be redirected to the logout.php page. However, in my current implementation, instead of redirecting, t ...

Information failed to load into the datatable

i've implemented this code snippet to utilize ajax for loading data into a datatable. However, I'm encountering an issue where the data is not being loaded into the database. $('#new_table').DataTable({ "processing": true, "ser ...

What is the best way to differentiate between a JSON object and a Waterline model instance?

Note: I have posted an issue regarding this on the Waterline repo, but unfortunately, I have not received a simpler solution than my current workaround. Within my User model, along with default attributes such as createdDate and modifiedDate, I also have ...

iPhone App Utilizing Twitter-JSON Parsing

Struggling with parsing JSON used to be a challenge for me until I discovered an excellent tutorial that guided me through the process. While creating my own web services to parse JSON, I followed the same steps successfully. However, when attempting to pa ...

What steps should I take to craft an SQL command that retrieves the data, specifically an image, stored within a JSON

JSON Object {"images": ["https://unique-example.com/image.jpg"], "remarks": "completed", "documentList": [{"id": "GST", "value": "Goods and Services Tax"}]} sele ...

Passing a return value to ajax success within an Express application

I am trying to retrieve a value from an included file and use it in the success function of an ajax call within Express. Below is my code for app.js: var userdetail = { "useremail":req.body.useremail, "fname" : req.body.fname, "lname" : req.bo ...

Interested in using jQuery to trigger the f12 key press?

In my current project, I have successfully disabled the f12 and right click using jQuery. Here is the code snippet: $(document).keydown(function(event){ if(event.keyCode==123){ return false; } else if(event.ctrlKey && event.shiftKey && ...

Show the checked items in the table based on the ID value stored in the array

When I click the button labeled Show checked if id values are 1 and 5, I encounter an issue in displaying checked items in the table based on their corresponding id value in the array. Essentially, I want to show the checked items in the table only if thei ...

Dealing with undefined props in a React component can be tricky. If you're running into issues with props being undefined

I'm encountering an issue where the props from getServerSideProps are showing as undefined when I attempt to pass them into a React component. Even though my code seems correct and everything works fine on the backend, I can't seem to determine ...

Static express is failing to load the node_modules folder

Why am I facing difficulties with my website not being able to use the node_modules directory on my server? const express = require('express'); const app = express(); app.use(express.static(__dirname + '/public')); app.listen(8080, &ap ...