0

I have following JavaScript Array

business: [{
      "id": 22,
      "name": "Private",
      "max_mem": 0,
      "gen_roomtype_id": 4,
      "status": 1,
      "type": 0,
      "set_value": 1
    },
    {
      "id": 6,
      "name": "Standard ward",
      "max_mem": 0,
      "gen_roomtype_id": 2,
      "status": 1,
      "type": 0,
      "set_value": 1
    },
    {
      "id": 7,
      "name": "Semi Private",
      "max_mem": 0,
      "gen_roomtype_id": 3,
      "status": 1,
      "type": 0,
      "set_value": 1
    }],      

"gen": [{
      "id": 5,
      "name": "laboratory",
      "description": "",
      "sharing": 0,
      "set_value": 2
    }],

And i have a idArray as following

idArray: [5, 7]

i would like to know whether the idArray values are belongs to "gen" Array or "business" Array.

4
  • 1
    what you have tried so far ? Commented Mar 14, 2018 at 13:39
  • what do u mean? unless they are both inside an array themselves you can't not know, beacause you need to loop that specific array to get the results Commented Mar 14, 2018 at 13:41
  • 2
    Please provide a minimal reproducible example - right now you have a snippet of some not understandable object Commented Mar 14, 2018 at 13:42
  • @Mr.Developer ,i have tried with for loop, i am not using JavaScript predefined functions Commented Mar 14, 2018 at 13:48

1 Answer 1

1

You can use the function every

This approach assumes the input data is an object.

var obj = {  business: [{      "id": 5,      "name": "Private",      "max_mem": 0,      "gen_roomtype_id": 4,      "status": 1,      "type": 0,      "set_value": 1    },    {      "id": 6,      "name": "Standard ward",      "max_mem": 0,      "gen_roomtype_id": 2,      "status": 1,      "type": 0,      "set_value": 1    },    {      "id": 7,      "name": "Semi Private",      "max_mem": 0,      "gen_roomtype_id": 3,      "status": 1,      "type": 0,      "set_value": 1    }  ],  "gen": [{    "id": 5,    "name": "laboratory",    "description": "",    "sharing": 0,    "set_value": 2  }]
};

var idArray = [5, 7];

var resultBusiness = idArray.every(n => obj.business.some(b => b.id === n));
var resultGen = idArray.every(n => obj.gen.some(b => b.id === n));

console.log("All in business: ", resultBusiness);
console.log("All in Gen: ", resultGen);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.