Modify array elements in JavaScript without the need to refresh the webpage

Upon loading the page, I am presented with a literal array as shown below:

<script type="text/javascript">
var members = [
    {
       name:"Alan Lim",
       id:"54700f06-a199-102c-8976-b1732b7ffc74",
       positions:[
          {
             id:"4cdeb2a2-8897-102d-80ee-95364de284f0"
          }
       ]
    },
    {
       name:"Ben Sinclair",
       id:"ed34b5a4-9b2f-102c-8475-9e610b13400a",
       conflict:"true",
       positions:[
          {
             id:"f00c2128-8895-102d-80ee-95364de284f0"
          },
          {
             id:"f00c68ea-8895-102d-80ee-95364de284f0"
          },
          {
             id:"4cde6824-8897-102d-80ee-95364de284f0"
          },
          {
             id:"4cde9ea2-8897-102d-80ee-95364de284f0"
          }
       ],
       locations:[
          {
             id:"88fb5f94-aaa6-102c-a4fa-1f05bca0eec6"
          },
          {
             id:"930555b0-a251-102c-a245-1559817ce81a"
          }
       ]
    },
    {
       name:"Debbie Wright",
       id:"fa49307a-9cfb-102d-bd08-842c500d506d"
    }
]
</script>

Is there a method to modify this array without triggering a page reload? For instance, I wish to add conflict:"true" to Alan Lim...

For example:

Replace this:

    {
       name:"Alan Lim",
       id:"54700f06-a199-102c-8976-b1732b7ffc74",
       positions:[
          {
             id:"4cdeb2a2-8897-102d-80ee-95364de284f0"
          }
       ]
    },

With this:

    {
       name:"Alan Lim",
       id:"54700f06-a199-102c-8976-b1732b7ffc74",
       conflict:"true",
       positions:[
          {
             id:"4cdeb2a2-8897-102d-80ee-95364de284f0"
          }
       ]
    },

I hope that clarifies the situation :) This adjustment is necessary because additional JavaScript code relies on extracting data from this array. Any modifications made by the other JavaScript need to be accurately reflected in this array...

Answer №1

To search for a specific member (most likely by name based on the query) and update their details, you can use a loop like this:

for(var j=0; j<members.length; j++){
  if(members[j].name === "Alan Lim"){
    members[j].status = "active";
  }
}

You can test it out here, or opt for a more general approach through this method:

function updateProperty(personName, property, value){
  for(var j=0; j<members.length; j++){
    if(members[j].name === personName){
      members[j][property] = value;
    }
  }
}

Answer №2

Given that your array is organized numerically, could you simply use the following line of code:

members[0]['conflict'] = "true";

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

Excessive form inputs extend beyond the modal when utilizing Bootstrap 3

Having an issue loading a modal with Angular and populating it with a template. The main problem I'm facing is that the inputs are extending beyond the boundaries of the modal - attached below is a screenshot illustrating the problem: Below is the c ...

What is the best way to retrieve data based on a condition using JavaScript within a React application?

I am struggling to load only a specific number of rows that meet a certain condition. Unfortunately, the current code is fetching all 25 rows and the condition is not being applied correctly. If anyone could provide assistance, it would be greatly apprec ...

Tips for implementing custom string ordering with Sequelize

Seeking guidance on implementing custom order with Sequelize I'm attempting to organize an array of objects fetched from the database as ACTIVE, PENDING, INACTIVE Any suggestions or examples for accomplishing this using Sequelize? Attempted approac ...

Exploring the Elements of Arrays in C#

I am looking to develop a property in C# that can either set or retrieve an individual element of an array. Currently, my implementation looks like this: private string[] myProperty; public string MyProperty[int idx] { get { if (myProperty ...

Ways to rearrange div elements using JavaScript

I need assistance with reordering div elements using JavaScript, as I am unsure of how to accomplish this task. Specifically, I have two divs implemented in HTML, and I would like the div with id="navigation" to appear after the div with class="row subhea ...

Efficient Loading and Smooth Scrolling with Angular2 (version 7)

I'm struggling to display a component upon the initial page load using lazy loading, where the content is only loaded when it's in view. For instance: - With 10 components on the page, I aim to show/scroll to component number 7 when the page lo ...

JavaScript: Working with Nested Callbacks and Retrieving MySQL Data

As I dive into the world of JavaScript server-side code, I find myself grappling with a common issue that many programmers face. In my previous experience with MySQL select queries in PHP, I would simply grab a result and loop through each row, performing ...

When performing a Numpy dot product, you may encounter an `IndexError` indicating that only integers, slices,

My current task involves calculating the dot product of two arrays using the following code snippet: for i in range(self.v_theta['shape'].shape[0] - 1): for j in range(self.v_theta['shape'].shape[1] - 1): self.theta['sha ...

Testing Components in Vue: A Guide to Mocking Vue Components

I have experience using React Testing Library, but I haven't used Vue Testing Library yet. I want to avoid using mount or shallowMount when testing components in VTL and figure out how to provide a stub instead. When testing a component like Componen ...

Trouble persisting in loading PopperJS and Bootstrap through RequireJS, despite following the suggested PopperJS version

Struggling to load jquery, popperjs, and bootstrap (v4-beta) using requirejs, I'm consistently encountering this error in the console: Uncaught Error: Bootstrap dropdown require Popper.js (https://popper.js.org) at bootstrap.js:6 at bootstrap ...

Tips for modifying the status of a single component within a v-for loop without impacting other components

I am encountering an issue with my childComponent that is dynamically rendered using v-for within the parentComponent. The Parent component contains logic to fetch data from the database and display it on the screen. The fetch request query is dependent on ...

Data has been successfully acquired through the query, however, it cannot be accessed via the GraphQL API

After successfully building the API with Apollo Server and verifying its functionality in GraphiQL, I proceeded to make requests to the API from a front-end React app using Apollo Client. const [getUserPosts, { loading, error, data }] = useLazyQuery(GET_US ...

AngularJS factory architecture for multiple functions

It's not the exact data I'm using in my project, but I'm simplifying it for demonstration purposes. In my AngularJS app, I have a factory like this: myApp.factory('inputinfo', function () { var self = { test: function (in) { ...

Utilize HighCharts to seamlessly implement multiple series with the help of AJAX requests

I am facing an issue with plotting the performance results on HighCharts using AJAX. The problem lies with the .addSeries API call not functioning correctly. I need assistance in determining if my approach is correct. $(function () { $('#chart3&a ...

Tips on creating a stationary sidebar that ends at the container's edge

I currently have a container set up with two columns, where one column acts as a sidebar. JSFiddle The sidebar is positioned as fixed, and I've utilized jQuery to adjust the bottom property so that it roughly stays at the bottom of the container as ...

What is causing my reusable component to malfunction when used in conjunction with the "setInterval" function?

I have created a custom <Loader /> component in which I can pass the text it is rendering and the speed as props. The code for the component is shown below: class Loader extends Component { constructor(props) { super(props); this.state = { ...

numerous ajax requests and array variables

I find myself in an interesting situation with my application, where I need to handle a couple of scenarios. Firstly, I have to fetch data from two different sources using AJAX calls. Secondly, I need to compare and manipulate this data. If the values matc ...

Encountering issues with React context API where the state is being set to undefined

I've recently delved into the world of React and have been utilizing the context API to manage my global state. Within the MyProvider.js file, I create a provider that simply holds 2 arrays of JSON objects. import {MyContext} from "./MyContext"; imp ...

What is the process of invoking a function from a different component in Vue 3?

I attempted to use the $on method and this.$root.$refs.compname_component = this;, but encountered some errors. Please see my code below: formComponent.vue <template> <div v-if="showForm"> create Form </div> </templa ...

Eliminate any repetitive items from the array while simultaneously boosting their quantity

Here is a list that I have: const myList = [ { name: 'AA', quantity: 1 }, { name: 'AA', quantity: 1 }, { name: 'BB', quantity: 1 }, { name: 'CC', quantity: 1 }, { name: 'CC', quantity: 2 }, ] I w ...