-4

I have an Array of Objects and each object has a status property,It may be pass or Fail or Skipped.If the Object status is other than 'pass', then i have to set the array status has fail irrespective of the number of objects in array and irrespective object with status pass.

 singleTestMethod=[
{'status':'PASS'},
{'status':'PASS'},
{'status':'FAIL'},
{'status':'SKIPPED'},
'arrayStatus':'']

i expect the array status should only be set to pass when all the object status in the array are pass otherwise it should be set to fail only.

  • 3
    What did you tried? Add your piece of code to the question, not just the array – reisdev Apr 05 '19 at 12:45
  • iterate the array until you get to the end or reach a fail status. If it is the latter, set the array status to false, else set the array status to true. – Farhan Qasim Apr 05 '19 at 12:47
  • please add the wanted result (as code) and waht you have tried. btw, your array can not have a property in literal notation. – Nina Scholz Apr 05 '19 at 12:47
  • Possible duplicate of [check if object property are all false](https://stackoverflow.com/questions/53433162/check-if-object-property-are-all-false) – adiga Apr 05 '19 at 12:50
  • Is your array valid? `:` is a syntax error within the scope of an array `[]` declaration. – esqew Apr 05 '19 at 12:54

3 Answers3

0

I think that you made a typo in writing your array, I took the freedom to fix it.

Because of a lack of clarity in your request I also assume that your structure will always be identical so 'arrayStatus' will be the last item of the array.

What I understood is that you want to check all the 'status' values in the array.

If all are 'PASS' then 'arrayStatus' must become 'PASS' too, otherwise 'arrayStatus' must become 'FAIL'.

If the above assumptions are right please try the following code:

var singleTestMethod=[
{'status':'PASS'},
{'status':'PASS'},
{'status':'FAIL'},
{'status':'SKIPPED'},
{'arrayStatus':''}];

console.log("singleTestMethod array before its check:")
console.log(singleTestMethod);

function setArrayStatus(myArray){
  var totalItems = myArray.length - 1;
  var totalPass = 0;
  myArray.forEach(function(entry) {
    if (entry.status === "PASS"){
   totalPass++;
  }
  });
  if (totalItems === totalPass){
      myArray[myArray.length - 1] = {'arrayStatus': 'PASS'};
    } else {
      myArray[myArray.length - 1] = {'arrayStatus': 'FAIL'};
    }
  return myArray;
}

singleTestMethod = setArrayStatus(singleTestMethod);

console.log("singleTestMethod array after its check:")
console.log(singleTestMethod);
Pitto
  • 8,229
  • 3
  • 42
  • 51
0

As what I understand, you want to know if all the objects has a status of pass, else it is considered fail right?

Then:

const testArr1 = [
{status:'PASS'},
{status:'PASS'},
{status:'FAIL'},
{status:'SKIPPED'},
{arrayStatus:''}]

const testArr2 = [
{status:'PASS'},
{status:'PASS'}]

const checkArray = (testArr) => !testArr.find(each => each.status !== 'PASS')

console.log(checkArray(testArr1)) // false
console.log(checkArray(testArr2)) // true
Lelouch
  • 910
  • 6
  • 19
0

Thanks for your answers,i have tried it using the object filter property has follows..

var singleTestMethod = [
{'status':'PASS'},
{'status':'FAIL'},
{'status':'SKIPPED'},
{'arrayStatus':''}];

const result = singleTestMethod.filter(singleTestMethod => 
 singleTestMethod.status === 'FAIL' || singleTestMethod.status === 'SKIPPED');

console.log(result);
if(result.length == 0){
  singleTestMethod.arrayStatus = 'PASS';
}else{
  singleTestMethod.arrayStatus = 'FAIL';
}
console.log( singleTestMethod.arrayStatus);