Increment by 1 when using .map and a condition evaluates to true

Related searches

Im having a hard time incrementing by 1 when I use .map to loop through array myClasses and when status in the array is equal to 'Open' or 'In Progress' increment a number by 1. My goal is to have the total number of classes that are either 'Open' or 'In Progress'

This is what Im currently doing => (a.status === ('Open' || 'In Progress') ? 'Yes' : 'No'))

currently when a class is Open or In Progress I just output either a yes or no but I want to be able to add a 1 or 0 to a number variable.

This is what myClasses array contains

0: {id: "0000AK",  type: "Elective", status: "Open"       } 
1: {id: "597WUK",  type: "Core",     status: "Closed"     } 
2: {id: "082HCE",  type: "Core",     status: "In Progress"} 
3: {id: "091HSN",  type: "Elective", status: "Canceled"   } 
4: {id: "038GAQ",  type: "Core",     status: "Open"       } 

Right now my output looks like this Yes,No,Yes,No,Yes instead I want the output to be 3

Any suggestions?

If its just the count you're after, you could simply use filter and get the length of the array returned

const myClasses = [{id:"0000AK",type:"Elective",status:"Open"},{id:"597WUK",type:"Core",status:"Closed"},{id:"082HCE",type:"Core",status:"In Progress"},{id:"091HSN",type:"Elective",status:"Canceled"},{id:"038GAQ",type:"Core",status:"Open"}];

const count = myClasses.filter(a => a.status === 'Open' || a.status === 'In Progress').length


JavaScript Loops Explained: For Loop, While Loop, Dowhile Loop , A loop will continue running until the defined condition returns false . The three most If it evaluates to true, the loop's statement is executed. Usually used to increment a counter. Map. var m = new Map(); m.set(1, "black"); m.set(2, "red"); for (var n of m) { console.log(n); } // Output: // 1,black // 2,red� If the condition evaluates true, the object is selected. Otherwise, it will be ignored. Similar to map, the filter is also an intermediate operation, which means you can call other Stream methods

You could take the count into the true part of the ternary.

var myClasses = [{ id: "0000AK", type: "Elective", status: "Open" }, { id: "597WUK", type: "Core", status: "Closed" }, { id: "082HCE", type: "Core", status: "In Progress" }, { id: "091HSN", type: "Elective", status: "Canceled" }, { id: "038GAQ", type: "Core", status: "Open" }],
    count = 0,
    result ={ status }) => status === 'Open' || status === 'In Progress'
        ? (count++, 'Yes')
        : 'No'


Loops: while and for, Any expression or variable can be a loop condition, not just comparisons: the condition is evaluated and converted to a boolean by while . If you’re starting in JavaScript, maybe you haven’t heard of .map(), .reduce(), and .filter().For me, it took a while as I had to support Internet Explorer 8 until a couple years ago.

What about ? Array reduce

const data = [
  { id: '0000AK', type: 'Elective', status: 'Open' },
  { id: '597WUK', type: 'Core', status: 'Closed' },
  { id: '082HCE', type: 'Core', status: 'In Progress' },
  { id: '091HSN', type: 'Elective', status: 'Canceled' },
  { id: '038GAQ', type: 'Core', status: 'Open' }

const res = data.reduce((acc, cur) => {
  const status = cur.status;
  if (status === 'Open' || status === 'In Progress') {
    return acc + 1;
  return acc;
}, 0);

FOR Statement with Variable Increment, The FOR statement executes one or more statements repeatedly, incrementing or decrementing a variable with each repetition, until a condition is met. @Hank Gay. As a follow-up to my own (rather useless) comment: Trove looks like the way to go. If, for whatever reason, you wanted to stick with the standard JDK, ConcurrentMap and AtomicLong can make the code a tiny bit nicer, though YMMV.

I suggest that you use reduce() instead of map():

var count = myClasses.reduce((accumulator, item) => {
    if (item.status == 'Open' || item.status == 'In Progress') {
        return accumulator + 1;
    return accumulator;
}, 0);

Note how I have also modified the condition in the if statement to compare against each of the two values that you want.

4. Algorithms and Flow Control, If the pretest condition evaluates to true , then the body of the loop is executed. the value in a local variable, and then using that variable in the control condition: You can also increase the performance of loops by reversing their order. Lookup tables are most useful when there is logical mapping between a single key� condition - Expression that is checked prior to the execution of every iteration. If omitted, this expression evaluates to true. If it evaluates to true, the loop’s statement is executed. If it evaluates to false, the loop stops. final-expression - Expression that is run after every iteration. Usually used to increment a counter.

Loops in Ruby, The simplest way to create a loop in Ruby is using the loop method. loop useful_loop.rb i = 0 loop do i += 1 puts i break # this will cause execution to Next, let's look at adding conditions within a loop by printing all even numbers from 0 up to 10. through the loop, but on the 5th iteration, the if statement evaluated to true� Initially, the for statement performs initialization action, then it checks the condition. If the condition is true, it executes actions, thereafter it performs increment or decrement operation. The loop execution continues as long as the condition is true. For instance, the following example prints 1 to 5 using for loop − Example

(1) IF condition – Set of numbers. Suppose that you created a DataFrame in Python that has 10 numbers (from 1 to 10). You then want to apply the following IF conditions: If the number is equal or lower than 4, then assign the value of ‘True’ Otherwise, if the number is greater than 4, then assign the value of ‘False’

True/False: The While loop will never execute if its condition is true to start with. False The loop that iterates as long as a condition is false and then stops when the condition becomes true is called a(n) ________ loop.

  • ('Open' || 'In Progress') will always evaluate to "Open" because it's a truthy value. You should use if(['Open', 'In Progress'].includes(a.status)).
  • Your condition a.status === ('Open' || 'In Progress') is wrong. Rewrite it as (a.status === 'Open' || a.status === 'In Progress')
  • why do you need to take map, if you like to get a single value?
  • In addition to the change to the comparison that others suggested, you should also use filter() instead of map() here. Then the count is just the length of the resulting array. Alternatively you can use reduce() to sum a counter.