What is the method for handling an array in Angular when all of them are successfully passed?

tasks: [
{failed: true, remarks: "",task: {'name': 'task1'}},
{failed: true, remarks: "",task: {'name': 'task2'}},
]

Is there a way to determine the overall status of all tasks based on their success or failure?

For instance, if all tasks have failed: true, then the overall status should be FAILED

If all tasks have failed: false, then the overall status should be PASSED

However, when the data is arranged as follows:

   tasks: [
    {failed: failed, remarks: "",task: {'name': 'task1'}},
    {failed: true, remarks: "",task: {'name': 'task2'}},,
    {failed: true, remarks: "",task: {'name': 'task3'}}
    ]

In this scenario, the overall status would automatically be set to FAILED

Answer №1

The pivotal requirement in your query is,

if all of them failed: false then it should be APPROVED

You must account for that scenario, with every other case resulting in DECLINED

You have the option to utilize the every iterator,

const decision = tasks.every(task => task.failed === false) ? 'APPROVED' : 'DECLINED';

let tasks = [{
    failed: true,
    remarks: "",
    task: {
      'name': 'task1'
    }
  },
  {
    failed: true,
    remarks: "",
    task: {
      'name': 'task2'
    }
  },
];

console.log(tasks.every(task => task.failed)); // output: true

let tasksWithFailed = [{
    failed: false,
    remarks: "",
    task: {
      'name': 'task1'
    }
  },
  {
    failed: true,
    remarks: "",
    task: {
      'name': 'task2'
    }
  },
];

console.log(tasksWithFailed.every(task => task.failed)); // output: false

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

Utilize the filter function to refine and sort through objects

I have an array of objects structured as follows: pages= [ { "id":1, "name":"name1", "languages":[ { "id":1, "lang":"en" }, { "id":2, "lang":"de" } ...

Why won't my navigation bar stay in place when I scroll down on the screen?

I'm trying to create a sticky navigation bar that becomes fixed when the user scrolls down to 200 pixels, but it's not working properly. I want it to behave like in this example: import React,{useState,useEffect} from 'react' functio ...

What is the best way to locate a particular JSON key value pair through JavaScript?

I'm in the process of creating a unique app that forecasts the weather for an upcoming event based on the city location specified. To achieve this, I am utilizing two APIs - one API provides an array of objects representing each event by an artist ent ...

Troubleshooting: Why isn't my Ajax post functioning correctly with PHP variables?

After researching similar questions, I have discovered that in order to send a JavaScript value to a PHP variable, AJAX must be used. Here is what I have tried: function onCursorChanged(e, data) { $.post('familytree.php', {id: data.context.i ...

ReactJs - SyntaxError: The JSX content is not properly terminated

I'm just starting out with ReactJs and have come across an issue that I can't seem to figure out. Here's my code snippet: var ListComponent = React.createClass({ render: function() { return ( <li>{this.props.va ...

How can I ensure that Bootstrap Navbar elements are all aligned in a single line?

https://i.sstatic.net/pkxyI.pngI'm experiencing an issue with my code, and I suspect it might be related to Bootstrap. I've tried using align-items-center but it doesn't seem to work as expected. Changing the display property to inline or in ...

The onload event for embedding SVG files is not functioning as expected

I created an angular directive: <div> <embed ng-src="{{url}}" id="map" type="image/svg+xml/> </div> I am trying to trigger a function when the svg is fully loaded. To achieve this, I have added an onload action listener durin ...

Designing a query feature to explore a Sequel Database utilizing a 'RETRIEVE' approach

In my index.erb, I've created a search bar with the following code: <nav> <div class="nav-wrapper"> <form> <div class="input-field"> <input id="search" type="search" placeholder="Search..." required> ...

Issue with managing cursor location using readline in Node.js

Currently, I am developing a console application in Node.js utilizing the readline module to manage cursor positions and obtain user input. Below is a custom library I have created for this purpose: // ReadLine.js const readline = require("readline&qu ...

What is the best way to showcase the output of a Perl script on a webpage

I recently came across a resource discussing the process of executing a perl script from a webpage. What is the best way to run a perl script from a webpage? However, I am facing a situation where the script I have takes more than 30 seconds to run and d ...

swap between style sheets glitching

My website features two stylesheets, one for day mode and one for night mode. There is an image on the site that triggers a function with an onclick event to switch between the two stylesheets. However, when new visitors click the button for the first ti ...

What could be causing Next.js to throw an error upon completion of the MSAL OAuth process?

I encountered an error while building a website using next.js. The site is set up for production, and after the authentication process with MSAL for Azure AD integration, I am facing the below error during the OAuth loop. As a beginner in next.js coming fr ...

A TypeScript function designed to only process arrays consisting of objects that contain a specific property determined by another parameter, with the requirement that this property

function retrieveObjectRow<T = string>( arrayData: { [key: T]: number; [key: string]: unknown; }[], targetValue: number, specifiedField: T ): typeof arrayData[number] | null { for (let i = 0; i < arrayData.lengt ...

Tips for creating a cohesive group of HTML elements within an editable area

Imagine having a contenteditable area with some existing content: <div contenteditable="true"> <p>first paragraph</p> <p> <img width='63' src='https://developer.cdn.mozilla.net/media/img/mdn-logo-s ...

Dayjs is failing to retrieve the current system time

Hey everyone, I'm facing an issue with using Dayjs() and format to retrieve the current time in a specific format while running my Cypress tests. Despite using the correct code, I keep getting an old timestamp as the output: const presentDateTime = da ...

Deactivating upcoming weeks according to the year in Angular 8

In the user interface, there are dropdowns for selecting a year and a week. Once a year is selected, the list of weeks for that year is displayed in the week dropdown. Now, the requirement is to disable the selection of future weeks. For example, for the ...

Enhance Your jQuery Skills by Adding Custom Directories to Anchor Links

Can jQuery be used to add a custom folder name in front of all links on a webpage? For example, if the website has these links: <a href="/user/login">Login</a> <a href="/user/register">Register</a> <a href="/user/forum">Foru ...

Execute a separate function when clicking on certain buttons without interfering with the buttons' original onclick function (which has already been assigned) - JavaScript

While I am still relatively new to JavaScript and learning, I have developed a webpage with multiple buttons that trigger different functions on click events. However, I would like to additionally call another function when any of these buttons are clicked ...

Is there a way to verify the presence of months in a column related to departments?

Is there a way to validate whether the current row aligns with the current column month and year? If not, can it be set to 0. Let's consider the current dataset. Presenting my resultData https://pastebin.com/GHY2azzF I want to verify if this data ...

Using React to iterate over an array of objects and generate Date TextFields in Material UI

I have an array of objects representing different stages in a specific process, each stage identified by an id and name. The structure of the array is as follows: const stages = [ { id: 1, name: initialize }, { id: 2, name: execute ...