Iterate through an array, mapping the values and then returning a

Look at the code provided:

const {
      aForm,
      bForm,
      eForm,
      qForm,
    } = this.form;

return (
      aForm.isEditing ||
      bForm.isEditing ||
      eForm.isEditing ||
      qForm.isEditing
    );

Can we optimize this in a different way? Perhaps something like this:

const forms = pick(this.form, [
      "aForm",
      "bForm",
      "eForm",
      "qForm",
    ]);
Object.values(forms).map(f => f.isEditing).join("||") //Need to return boolean value.

Answer №1

In my opinion, the existing setup you have is pretty close to what most viewers would expect. I recommend utilizing Array#some for this task. It allows you to verify if at least one of the items meets the specified condition (similar to a logical OR ||).

Your current function functions in the opposite manner as it checks each value with every item (resembling a logical AND &&).

const properties = [
  "aForm",
  "bForm",
  "eForm",
  "qForm",
]

return properties.some(prop => {
  const obj = this.form[prop] // Retrieve your form element

  return obj.isEditing;
})

Answer №2

let formArray = [
  "aForm",
  "bForm",
  "eForm",
  "qForm",
];
const result = Object.values(formArray).map(form => eval(form).isEditing).join("||");
eval(result);

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

Display a message indicating no data is available if the specified text is not found within the div

In the code snippet below, there is an input element followed by a div containing multiple child elements: <input type="text" onkeyup="filter()" id="filter_data"> <div id="body"> <div class="child"> Text 1 </div> <div class ...

Utilizing Ajax to serialize or transfer JSON objects

I have received a Json object and I am looking to extract the data using JavaScript. Specifically, I need help with looping through fields and extracting the data. def For_Sale_Listing(request,id): try: listing = Listing.objects.filter(pk=id) ...

Struggling to maintain context with axios in React despite diligent use of arrow functions

In my component, I have a function for posting data. Although it works well, the context of my component is lost in the success message. This is puzzling because I am using arrow functions. Why does the "this" context get lost in this situation? The issu ...

Obtain the names of the cities that the Google Maps route passes through

Currently working on a website that integrates Google Map API v3. Is there a method to gather the names of the places or cities along the route from point A to B? The page utilizes JavaScript for functionality. ...

Retrieving data from a JSON file depending on the selection made by the user in a dropdown menu

In my project, I have a JSON file named countries.json that contains country and regional information. Users are required to select a country using a Python-generated dropdown list from this file. However, due to the backend nature of Python, I am unable t ...

Encounter an issue during npm installation of electron: "Error verifying the initial certificate."

I recently developed a new app directory and ran the command npm init. However, I encountered an issue while trying to install Electron with the following line of code: npm install electron --save-dev. The error message I received is as follows: > [em ...

Tips for receiving responses when data is posted to a URL in Node.js using `req.body.url`

How can I retrieve data from the URL provided in req.body.url using Postman's collection tool? In my code snippet, I have defined a key as "URL" and the corresponding value as a live URL. You can refer to the screenshot below for more details: const ...

What is the best way to maintain the index of a for loop when incorporating AJAX to insert PHP data into an object?

Hey there, I'm diving into the world of AJAX and PHP implementation. I've hit a bit of a roadblock lately as I feel like I might be missing a simple solution. Currently, my code fetches data from a trove API using PHP, and for each item it appen ...

Ways to display "No records" message when the filter in the material table in Angular returns no results

How can I implement a "No Records Message" for when the current table is displaying empty data? Check out this link for examples of material tables in AngularJS: https://material.angular.io/components/table/examples ...

Step-by-step guide to creating a custom wrapper in React that modifies the props for a component

Exploring React components for the first time and seeking assistance. I am interested in dynamically wrapping one component inside another and modifying its props. For instance, considering the following component: If we want to pass the key3 from a wrapp ...

Using Ruby variable as a parameter in JavaScript

Is there a way to include a ruby variable as a parameter in a JavaScript function for a Rails checkbox? Here is an example of what I'm trying to do: <%= check_box_tag 'Sugestão', prato.id , prato.sugestao,:class => prato.categoria_pr ...

CSS swapping versus CSS altering

Looking to make changes to the CSS of a page, there are two methods that come to mind: Option 1: Utilize the Jquery .css function to modify every tag in the HTML. For instance: $("body").css("background : red") Alternatively, you can disable the current ...

Unable to properly access required file path through HTML source

I have a confidential folder named 'inc' where I store sensitive files such as passwords in php connection files. This folder is located at the same level as the 'public_html' folder. I successfully accessed php files with database conn ...

Using AngularJS to add a unique custom class directive to each item in an ng-repeat loop

Can anyone help me figure out why the width of a div isn't being set dynamically using AngularJS? <ul id="contents"> <li ng-repeat="content in contents"> <div class="content_right custom_width"> <div class="title"> ...

unable to press the electron button

I am currently working on a project that involves connecting PCs together for screencasting. While following an online coding tutorial, I encountered an issue with clicking the button to generate the ID code. Here is the code snippet from app.js: // Code ...

How can I make a POST request from one Express.js server to another Express.js server?

I am encountering an issue while trying to send a POST request from an ExpressJS server running on port 3000 to another server running on port 4000. Here is the code snippet I used: var post_options = { url: "http://172.28.49.9:4000/quizResponse", ti ...

Using Express.js to transform req.body into a POST encoded string

I need to convert the req.body object into a POST encoded string using the express.bodyParser middleware. Is there a way to achieve this? For example: Name: Jane Doe Age: 30 City: Los Angeles Should become: Name=Jane+Doe&Age=30&City=Los+Angeles ...

SPRING --- Tips for sending an array of objects to a controller in Java framework

Can someone help me with this issue? I am using AngularJS to submit data to my Spring controller: @RequestParam(value = "hashtag[]") hashtag[] o The above code works for array parameters but not for an array object. This is my JavaScript script: $http ...

Embedding an Iframe in Angular 2 directly from the database

Looking for assistance with iframes in Angular 2. Initially, embedding an iframe directly into a component's template functions correctly. <iframe src='http://plnkr.co/edit/zZ0BgJHvQl5CfrZZ5kzg?p=preview | safeUrl' allowtransp ...

Why is TypeScript unable to recognize package exports? (using CommonJS as the module system and Node as the module resolution)

I have an NPM package that is built for ESM and CJS formats. The package has a dist folder in the root directory, which contains: dist/esm - modules with ESM dist/cjs - modules with CJS dist/types - typings for all modules In the package.json file, there ...