Performing JSON CRUD operations with vanilla JavaScript

Looking to update the content_available value to true for codes EN and CN in a JSON object. Here is the attempted solution:

myObj = { "data": [{ "code": "EN", "language": "English", "content_available": true, "isdefault": true }, { "code": "AR", "language": "Arabic", "content_available": true, "isdefault": false, "default" : true

    }, {
        "code": "BR",
        "language": "Brazilian Portuguese",
        "content_available": true,
        "isdefault": false
    }, {
        "code": "CN",
        "language": "Simplified Chinese",
        "content_available": true,
        "isdefault": false,
                "default" : true
    }, {
        "code": "TW",
        "language": "Traditional Chinese",
        "content_available": true,
        "isdefault": false
    }, {
        "code": "DE",
        "language": "German",
        "content_available": true,
        "isdefault": false
    }, {
        "code": "ES",
        "language": "Spanish",
        "content_available": true,
        "isdefault": false
    }, {
        "code": "FR",
        "language": "French",
        "content_available": true,
        "isdefault": false
    }, {
        "code": "JP",
        "language": "Japanese",
        "content_available": true,
        "isdefault": false,
                 "default" : true
    }, {
        "code": "RU",
        "language": "Russian",
        "content_available": false,
        "isdefault": false
    }],
    "success": true
    }

 function setContentAvailable() {
     for (var key in myObj.data) {
         if (myObj["data"]["code"] === "EN" && myObj[data][code] === "CN") {
             myObj.data.content_available = false;
         }
     }
 }
 setContentAvailable();
 console.log(myObj);

Answer №1

Your existing code utilizes a for loop, however, it overlooks the use of the key parameter. Also, the condition check for “EN” or “CN” should be done using || (or) instead of && (and).

To ensure that content_available is updated as true for codes EN and CN, you can update the code as follows:

 function setContentAvailable() {
   for (var key in myObj.data) {
     if (myObj["data"][key]["code"] === "EN" || myObj["data"][key]["code"] === "CN") {
         myObj["data"][key]["content_available"] = true;
     }
   }
 }
 setContentAvailable();

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

Retrieving JSON data from a controller method and passing it to jQuery

I've been working on getting this functionality to work properly for the past two days. My goal is to create a log-in feature where I invoke the controller action from jQuery, passing it a JSON object (using json2.js), and then receiving a JSON object ...

I am facing issues with getting the delete function to properly function in my React

Issue with Delete Button Functionality in React While I am able to successfully delete an item using axios call in Postman, implementing the same functionality on the front-end in a React component seems to be causing problems for me. <button onSubmit ...

What is the best way to create dynamic .env files that can easily adapt to different environments instead of

Having multiple .env files (one for Vue and one for Laravel) with 'localhost' hard coded in them is causing accessibility issues from other computers on my network. It would be beneficial to have this set up dynamically, except for production. F ...

Implementing a Unique Approach to Showcase the Initial PDF Page as Cover through Django and JS

I would like to enhance my script so that when a user hovers over an object on the template, a PDF cover page can be set for each object. Current process: Currently, I am able to upload files in the .pdf and .epub formats for each object with additional ...

You have encountered an issue with the runtime-only build of Vue, which does not include the template compiler

Lately, I have been utilizing Vue in a project and encountered an issue where upon compiling, my browser page displays as white with an error message stating "You are using the runtime-only build of Vue where the template compiler is not available. Either ...

Toggle button with v-bind in Nativescript Vue

Hey there, I'm just starting out with nativescript vue and I have a question regarding a simple "toggle" feature that I'm trying to implement. Essentially, when a button is pressed, I want the background color to change. <template> < ...

The Bootstrap 3.3 Carousel is stationary and does not rotate

I am facing a challenge with implementing a Carousel using Bootstrap version 3.3.7 on my website. The code snippet is as follows: <!-- Carousel ================================================== --> <div class="row dark-start d-none d-lg-block"&g ...

Issues arise when attempting to transmit JSON data from JavaScript to the server using the POST method

I'm having trouble viewing the JSON data I sent to the server using XMLHttpRequest. It seems like the server isn't receiving it because when I run the JavaScript, the alert window pops up but doesn't display anything. Does anyone have a solu ...

Updating values in an Object by assigning them with results from an array

I've been grappling with a data structure issue in nodejs and I could really use some assistance. Here's the scenario: I have an object: let obj = { commenter: '', comment: '', numberOflikes: 0, } Along with a containe ...

Save the JSON data into a variable inside a React utility component

Currently, I am new to React and facing an issue with fetching data and storing it in a variable. I have been struggling to understand why my SetMovieResponse function is not working as expected. I have tried stringifying the JSON before sending it, but w ...

Encountering the error message "Module 'request' not found" despite the fact that I've already included the request module

I'm currently facing an issue with my Cloud Function that involves using the request library. After installing the request package using npm install request, I noticed it's located in the node_modules directory, just like all the other packages: ...

Creating collections in a Hashtable style using JavaScript

Creating a collection in JavaScript can be done in the following way: Start by initializing an empty collection with var c = {}; Next, you can add items to it. After addition, it will look like: { 'buttonSubmit': function() { /* do some work * ...

Combining multiple JSON objects into a single array in AngularJS

Currently, I am facing a challenge in merging two API calls. The first call involves fetching data by filtering the account_id on the backend, while the second call retrieves data based on the test_id. Let's start with the JSON response for /api/test ...

Is there a different term I can use instead of 'any' when specifying an object type in Typescript?

class ResistorColor { private colors: string[] public colorValues: {grey: number, white: number} = { grey: 8, white: 9 } } We can replace 'any' with a specific type to ensure proper typing in Typescript. How do we assign correct ...

Exploring Angular controllers, promises, and testing

Currently, I am in the process of writing some unit tests for my controller that utilizes promises. The code snippet in question is as follows: UserService.getUser($routeParams.contactId).then(function (data) { $scope.$apply(function () { $sco ...

Swap out the content in a text input box with the text chosen from a suggested autocomplete option

I am working on a text input box with auto-complete options displayed below it. I want to enable users to navigate through the options using keyboard arrows and "select" one, causing it to change color. How can I update the text in the input box with the s ...

Unique Text: "Personalized marker/pin for interactive map feature"

Looking to create a custom image map marker/pin with a unique bottom design resembling a union shape using CSS and Vue.js. I've attempted it myself but haven't been able to achieve the exact look as shown in the reference image. Any advice or ass ...

What is the best way to extract values exclusively from a JSON array using jQuery?

Struggling to extract values from a JSON array, I've scoured Stack Overflow for solutions but haven't found the right one. This is what my code looks like: .done(function (data) { var jdata = JSON.stringify(data['queryBuilder']); ...

Issue with firing Facebook pixel after router.push() in Next.js

Within this code block is FB pixel tracking code <Script id="some-id" strategy="afterInteractive">some fb pixel code</Script> The issue arises when navigating to a page containing the script using router.push(SOME_ROUTE). T ...

In the Vue mounted hook, the term "TradingView" has not been declared

I am unsure if this is the right place to ask this question, but I am currently using the tradingview library. While it is working for me, it is not functioning the way I intend it to. As per the documentation, I have placed my code in the index.html file ...