-1

I have the below response body (json) I want to do the below checks

1) Check if 'id=5' exist

John
  • 1
  • 3
  • Possible duplicate of [Find object by id in an array of JavaScript objects](https://stackoverflow.com/questions/7364150/find-object-by-id-in-an-array-of-javascript-objects) – matvs Nov 23 '19 at 19:09

2 Answers2

0

You can use some method to check whether array satisfies some conditions:

const result = data.some(s=> s.id == 5 && s.name =='John Smith');

const data = [
  {
    "id": 1,
    "name": "George Smith",
    "username": "John",
    "email": "John@may.biz",
    "address": {
      "street": "Kennedy",
      "suite": "Apt. 332",
      "city": "Paris",
      "zipcode": "12322-3874",
      "geo": {
        "lat": "-57.3189",
        "lng": "81.1496"
      }
    },
    "phone": "1-999-736-444 x343343",
    "website": "web.org",
    "company": {
      "name": "Amaon",
      "catchPhrase": "Multi-layered",
      "bs": "e-markets"
    }
  },
  {
    "id": 5,
    "name": "John Smith",
    "username": "johny",
    "email": "johny@john.com",
    "address": {
      "street": "Oxford",
      "suite": "Suite 334",
      "city": "London",
      "zipcode": "12345",
      "geo": {
        "lat": "-32.3434",
        "lng": "45.4543"
      }
    },
    "phone": "(121)954-3457",
    "website": "smith.info",
    "company": {
      "name": "Law LLC",
      "catchPhrase": "Customer first",
      "bs": "E2E system"
    }
  }
]


const result = data.some(s=> s.id == 5 && s.name =='John Smith');
console.log(`Has John Smith: ${result}`)

const hasJoseph = data.some(s=> s.id == 5 && s.name =='Joseph');
console.log(`Has Joseph: ${ hasJoseph }`);
StepUp
  • 36,391
  • 15
  • 88
  • 148
  • cannot add "const data = [", the response is coming as it is – John Nov 23 '19 at 15:06
  • @John do you have an array of the above data? – StepUp Nov 23 '19 at 15:07
  • @John and just use `some` method in JavaScript code – StepUp Nov 23 '19 at 15:23
  • var jsonData = pm.response.json(); const result = jsonData.some(s=> s.id == 5 && s.name =='John Smith'); tests["Result: " + result] = true; it is returned 'passed' with any name I put – John Nov 23 '19 at 16:02
  • @John I've just added some tests to my code. Check it, pls – StepUp Nov 23 '19 at 16:09
  • 1
    //test pass var jsonData = pm.response.json(); const result = jsonData.some(s=> s.id == 5 && s.name =='John Smith'); tests["Result: " + result] = result === true; //test failed var jsonData = pm.response.json(); const result = jsonData.some(s=> s.id == 8 && s.name =='John Smith'); tests["Result: " + result] = result === true; @StepUp, thanks brother, really appreciate it – John Nov 23 '19 at 16:24
  • @John I am glad that it helped to you!:) [What should I do when someone answer](https://stackoverflow.com/help/someone-answers) – StepUp Nov 23 '19 at 16:31
0

function myFunction() {
var ages = [
  {
    "id": 1,
    "name": "George Smith",
    "username": "John",
    "email": "John@may.biz",
    "address": {
      "street": "Kennedy",
      "suite": "Apt. 332",
      "city": "Paris",
      "zipcode": "12322-3874",
      "geo": {
        "lat": "-57.3189",
        "lng": "81.1496"
      }
    },
    "phone": "1-999-736-444 x343343",
    "website": "web.org",
    "company": {
      "name": "Amaon",
      "catchPhrase": "Multi-layered",
      "bs": "e-markets"
    }
  },
  {
    "id": 5,
    "name": "John Smith",
    "username": "johny",
    "email": "johny@john.com",
    "address": {
      "street": "Oxford",
      "suite": "Suite 334",
      "city": "London",
      "zipcode": "12345",
      "geo": {
        "lat": "-32.3434",
        "lng": "45.4543"
      }
    },
    "phone": "(121)954-3457",
    "website": "smith.info",
    "company": {
      "name": "Law LLC",
      "catchPhrase": "Customer first",
      "bs": "E2E system"
    }
  }
];
  ages.filter(function(obj) {
      return obj.id == 5 && obj.name == 'John Smith';
    })
    .map(function(obj) {
     console.log(obj)
      return obj;
    });
}
//This method will return the object that matches your condition.
myFunction();

Hope this helps :)

Ayyub Kolsawala
  • 809
  • 8
  • 15