Utilize Javascript to establish a fresh attribute by analyzing other attributes contained in an array within the object

Currently, I am working on a data structure that looks like this:

masterObject: {
  Steps: [
    {
      step: {
        required: false,
      },
      step: {
        required: false,
      },
      step: {
        required: false,
      },
    },
  ]
}

My goal is to iterate through the array of steps and verify if each step object contains the 'required' property set to false. If all steps are indeed not required, then I want to add a new property to the masterObject called

masterObject.isObjectWithNoRequiredSteps = true;

Once this code snippet runs, the updated masterObject will resemble this:

masterObject: {
  Steps: [
    {
      step: {
        required: false,
      },
      step: {
        required: false,
      },
      step: {
        required: false,
      },
    },
  ]
  isObjectWithNoRequiredSteps: true
}

If there is an instance where one of the step objects has the required property set to true, then I want to update the new property to false like so:

masterObject.isObjectWithNoRequiredSteps = false;

masterObject: {
  Steps: [
    {
      step: {
        required: true,
      },
      step: {
        required: false,
      },
      step: {
        required: false,
      },
    },
  ]
  isObjectWithNoRequiredSteps: false
}

I am seeking advice on the best method for implementing this functionality at the higher level object.

Answer №1

The Steps array contains objects with multiple attributes or keys named step, which is invalid in JavaScript. It is recommended to adjust the data structure as shown below

masterObject: {Steps: [{ 
    step1: { required: false, }, 
    step2: { required: false, }, 
    step3: { required: false, },
}, ] }

To check if each step within the Steps array has a value of false for the "required" attribute, you can use the following code snippet

masterObject.isObjectWithNoRequiredSteps = Object.keys(masterObject.Steps[0]).reduce(function(result, nextStep){
const isRequired = masterObject.Steps[0][nextStep].required; 
return !isRequired && result; 
}, 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

Automated Submission and Online Posting of (Web Form) with JavaScript Script

Is there a way to automatically fill out the form on my webpage ( ) using a JavaScript function with specific parameters? The source code for the webpage can be found here: webpage sourcecode script enter image description here ...

Using Vue: Save user information in the Vuex store prior to registration

Hello fellow members of the Stackoverflow Community, I am currently working on a Vue.js application that involves user registration. The registration process is divided into three components: Register 1 (email, password), Register 2 (personal information) ...

Combine iron-page and bind them together

Recently, I've started learning about Polymer and I want to bind together paper-tabs and iron-pages so that when a tab is clicked, the content loads dynamically. After going through the documentation, this is what I have tried: <app-toolbar> ...

Linking Angular with property of an object

Is there a way to ensure that the binding of object properties works properly? For instance, in my controller I have: $scope.reviews = { rating_summary: 4, items: [ { title: 'A Title'}, etc... ] } And in my view: <li ng-repeat="review i ...

Retrieve the text from an ajax response that includes the <tag> element

I have created a simple piece of code to fetch news from an RSS page. Below is the code snippet: this.loadRecentNews = function loadRecentNews() { $.get("http://rss.nytimes.com/services/xml/rss/nyt/GlobalHome.xml", function (data) { ...

Ways to incorporate ejs partials using JavaScript

I am currently developing code to determine if a user is logged in. Depending on the user's login status, the content of the "my user" section should vary. When a logged-in user navigates to the "my user" page, an if statement is executed to confirm ...

jQuery image resizing for elements

I have successfully adjusted the images in the gallery to display one per row for horizontal orientation and two per row for vertical orientation. Now, I am facing a challenge in making the images scalable so they can resize dynamically with the window. A ...

What could be causing React to throw an error?

Check out this React Component: GeneralInformation = React.createClass({ totalCaptialRaisedPanel: function() { var offeringInfo = this.props.company.data.offerings.data[0]; var percentageComplete = (offeringInfo.capital_raised / offer ...

Is it possible to disable 'rtl' using {flip: false}?

I am attempting to replicate the behavior demonstrated in the MUI docs at this link. When switching from 'ltr' to 'rtl', the 'Affected' content flips sides while the 'Unaffected' remains unaffected. To illustrate th ...

Update the content of a div on the WordPress homepage with the click of a button

Hey there, I'm currently working on customizing the Boutique theme for a website. My goal is to add two buttons to the home page that will display different sets of products when clicked. I've been attempting to use the visibility property, but h ...

Tips for showcasing a value from a separate controller in angularjs

Two of my modules are Authentication and Home, each with their own controllers. Both controllers share the same name: controllers.js. I am facing a challenge where I need to display the value $scope.username on the home page (within the Home module) which ...

Finding the value of an input without having to submit it first and searching for it within a datalist

> Here is an example of HTML code <label>Person</label> <input name="PersonID" type="text" id="PersonID"> <label>Car Plate Number</label> <input name="PersonsCarPlateNumber" list="PersonsCarPlateNumbe ...

Calculating the total number of days in each month within a specified date range using PHP in a dynamic way

Thank you for taking the time to help me with my issue. For instance, if a user selects a date range from 10 Dec 2016 to 20 April, how can I calculate the number of days for each month within that range? For example, Dec=22 Days, Jan=31 Days, Feb=28 Days, ...

EaselJS: Enhancing Performance and Aesthetics with Over 200 Vector Shapes

When comparing EaselJS performance with Canvas native methods, I noticed a significant difference: 2.2 s vs 0.01 s (despite EaselJS mapping canvas native methods). I created a canvas application that draws a tree*. For animating the growth of the tree, us ...

Using Ajax without implementing JavaScript

Is it possible to develop an application that utilizes Ajax without relying on JavaScript, allowing it to function even if JavaScript is disabled by the user in their browser? Are there any restrictions or limitations to consider? ...

Issue: Error encountered while trying to use the removeAttribute() function in Selenium and Java: missing closing parenthesis after argument list

WebElement removeReadOnly=driver.findElement(By.xpath("//input[@id='mat-input-0']")); js.executeScript("document.getElementBypath('//input[@id='mat-input-0']').removeAttribute('readonly');",&quo ...

Confirming angular 1.x input checkbox

In my code, I have an angular 1.x checkbox set up like this: <input type="checkbox" name="fooName" id="fooId" ng-model="false" > When I use jQuery to do the following: $("#fooId").val() I always receive "on" as the output. The same result oc ...

Tips for navigating to an external website through GraphQL

I am currently utilizing Node.js and Front-end on Next.js. I have a GraphQL server with a GetUrl method that returns a link (for example: ""). My goal is to redirect a client who made a request to that page with a Basic Auth Header. From what I understan ...

Tips for implementing fetch in a GET request and displaying the outcomes on the screen

Currently, I am utilizing express router in Node for an endeavor to display a page with data retrieved from an API. Unfortunately, no results are returned, and I can confirm that the issue does not lie with the API itself. Upon manual inspection in the con ...

When choosing an item in the typeahead feature, the selected value is empty

I'm currently integrating typeahead into an existing project, so I assume the issue lies within Below is my code snippet: <pre>Model: {{entry.value | json}}</pre> <input ng-show="entry.isToShow" class="form-control col-xs-12 " ng-mo ...