1

I have this array:

var data= [{IsNormal:"true", Name:"Mike"},
         {IsNormal:"true", Name:"Tom"},
         {IsNormal:"false", Name:"Clause"},
         {IsNormal:"true", Name:"Timm"},
         {IsNormal:"true", Name:"Marta"},
         {IsNormal:"true", Name:"Dora"}];

I need to write function to check if at least one of the objects in array has property IsNormal equal to false, if there is function must return false otherwise it must return true.

Here is my implementation:

    function chekStatus(data) {
        _.each(inspections, function (value, key) {
            if (!value.IsNormal)
                return false;
            return true;
        });
    }

but I want to write something more elegant using array prototype javascript fuctions.

0

2 Answers 2

3

Use Array.some

 function chekStatus(data) {
     return data.some(function (item) { return !item.IsNormal; })
 }

Note, IsNormal values should be boolean, not string

var data= [{IsNormal:true, Name:"Mike"},
     {IsNormal: true, Name:"Tom"},
     {IsNormal: false, Name:"Clause"},
     {IsNormal: true, Name:"Timm"},
     {IsNormal: true, Name:"Marta"},
     {IsNormal: true, Name:"Dora"}];

Working fiddle

Sign up to request clarification or add additional context in comments.

3 Comments

it's awayes return true!
@Michael the code works like a charm if data are boolean. In your example you have IsNormal that is a string, so you need to do return item.IsNormal === "false"
here is row: object.IsServiceable = inspectionReviews.some(function (item) { return !item.IsNormal });
1

You can use Array.some()

data.some(obj => !obj.IsNormal)

2 Comments

this sign not recognized =>
@Michael then the other answer, that is equal but doesn't use ES6, should work

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.