What is the process for updating a variable within a JSON file?

I am looking to update the state value in my json data. I am creating a game using HTML and JavaScript, and within my JSON file, I have players with attributes such as name, password, and state.

The task at hand is to specifically target the state property and change it from 1 to 3 if necessary.

How can I write code that will only modify the player.state from 1 to 3?

Below is an example of the JSON file:

[
    {"pseudo":"player1","password":"player","state":"1"},
    {"pseudo":"player2","password":"player","state":"0"}
]

Answer №1

Here is a simple way to achieve this:

function updatePlayerState(playersArray, playerToFind, newState){
     playersArray.find(function(player) {return player.pseudo == playerToFind}).state = newState;
}

var playersList = [
    {"pseudo":"player1","password":"player","state":"1"},
    {"pseudo":"player2","password":"player","state":"0"}
]

updatePlayerState(playersList,"player2",3);

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

Combining shared table rows with embedded ruby code in Javascript - is it possible?

I'm a new Javascript learner and I'm attempting to create a function that will merge rows with the same value (year in this case) and add their numbers together. Although my code isn't functioning as expected, it also doesn't seem to be ...

Creating time formats in React Native can be accomplished by utilizing the built-in date

I need to show the expiry date on two different screens, provided by an API. The first expiryDate is displaying in the correct format, but the second one is giving an "invalid time" error. Can you please advise on how to resolve this issue? // 1. This cod ...

Exploring JSON data structures universally with Haskell

I am looking to create a program in Haskell that can take the name of a JSON file and interpret the additional arguments as a path to navigate through the JSON file and output the value at that location. The challenge lies in the fact that JSON can contain ...

Store the filter value in a variable inside a directive

Can I use filters in directives as well as controllers? Imagine injecting the filter into a directive like this... app.directive('ngDirective', ['$compile','$filter', function ($compile, $filter) { 'use ...

What is the best method for generating a vertical tab using JavaScript in a paragraph format?

Struggling to create a menu similar to this using bootstrap 4. Despite my efforts, I keep encountering various issues. Is there someone who can help me solve this problem? Remember, I am using bootstrap 4. Here is a demo: <!doctype html> <html ...

Using Typescript to Import One Namespace into Another Namespace

Is it possible to export a namespace from one typescript .d.ts file and then import that namespace into another .d.ts file where it is utilized inside of a namespace? For instance: namespace_export.d.ts export namespace Foo { interface foo { ...

Utilize JSON.NET to deserialize JSON data that includes unconventional variables

As I am dealing with JSON data (where the format is out of my control), I am wondering if I can deserialize it using the JsonConvert.DeserializeObject method from JSON.NET library. "{'episodes':{'1':true,'2':true,'3&apos ...

Vue.js is able to update various models based on selection from a form dropdown

I have a dynamic select list in my app built with vue.js. I am trying to update a "details" box on the page with information fetched through an ajax request. You can view the code here: https://jsfiddle.net/pznej8dz/1/ I am puzzled as to why the sf_detail ...

Converting JSON array to a String and back to JSON in an Android application

Recently, I've been working on sending post data to my local PHP server. To make it easier to process the data later in PHP, I decided to format the post data as JSON. Using "Namevaluepairs", I converted my JSON array to a string and ended up with the ...

Using the fwrite() function, the script writes a JSON encoded array twice

Currently, I am working on generating a JSON file by collecting data through GET requests. The JSON file consists of a 2D array of strings, but there seems to be an issue where the nested array gets duplicated when writing the data to the file. <?php / ...

Navigating through scenes with just a few mouse clicks and drags

I'm having an issue with TrackballControls. It's not functioning properly and I can't figure out why. I'm following the example provided here: link to example. I've been searching for a solution but still can't seem to pinpoin ...

Mastering AngularJS Charts: Successfully Loading External JSON Data without Errors

Greetings! Allow me to apologize for what might seem like a basic question. I've recently delved into the realm of java, JSON, and all that jazz, and it's rather uncharted territory for me. I've scoured high and low but simply can't pin ...

Utilized OBJLoader to load an item successfully; now seeking guidance on calculating the coordinates of an element within (three.js)

I am utilizing three.js and OBJLoader to create a 3D human head representation: let renderer, camera, scene, head, light, projectiles; new THREE.OBJLoader().load(objUrl, initialize); function initialize(obj) { renderer = new THREE.WebGLRenderer({ al ...

Injecting data into a Q promise

I'm facing some issues related to what seems like JavaScript closures. In my Express + Mongoose web application, I am utilizing the Q library for Promises. I have a question regarding passing request data to the promise chain in order to successfully ...

How can we add hover effects to Ionic / Angular components when touching or clicking?

For my app, I need to replicate the hover effect from CSS on mobile devices. Users can click and drag elements, and when they drag over certain elements, I want those elements to change z-index and other styles. Is there a way in Angular / Ionic or even ju ...

Using jQuery to insert a div class with PHP referenced

My variable stores a color name. Here is an example: <?php $myvar = blueColour; ?> I want to apply this value to the body of an html page using jQuery: <script type="text/javascript"> jQuery(body).addClass("<?php echo $myvar; ?>"); < ...

Varied perspectives for Angular on Desktop versus mobile devices

In my current project, I'm creating an application that requires two completely different views for Desktop and Mobile. Due to the entirely different content, using CSS alone is not an option. What steps have been taken so far? I've checked whe ...

What factors influence the speed of an Ajax request?

It is common knowledge that the amount of data transmitted to the server, as well as the volume of information returned in a call can greatly affect its speed. However, what I am curious about is whether the following factors might also impact the transmi ...

What is the best way to extract a specific field from a JSON object using Node.js?

Here is how the JSON object looks with the TEMP field that I am attempting to retrieve: [ [ { "DEVICE_ID": "357807272", "TEMP": 20.439 } ] ] ...

Vue.js component function "not instantiated within the object"

I recently started learning vue.js and decided to use vue-cli to set up a new project. As I was working on adding a method to a component, I encountered some issues: <template> <div> <div v-for="task in $state.settings.subtasks& ...