-2

I have the following Object

var test = {
  "Employees": [
    {
      "userId": "rirani",
      "jobTitleName": "Developer",
      "firstName": "Romin",
      "lastName": "Irani",
      "preferredFullName": "Romin Irani",
      "employeeCode": "E1",
      "region": "CA",
      "phoneNumber": "408-1234567",
      "emailAddress": "romin.k.irani@gmail.com",
      "active" : true
    },
    {
      "userId": "nirani",
      "jobTitleName": "Developer",
      "firstName": "Neil",
      "lastName": "Irani",
      "preferredFullName": "Neil Irani",
      "employeeCode": "E2",
      "region": "CA",
      "phoneNumber": "408-1111111",
      "emailAddress": "neilrirani@gmail.com",
      "active" : true
    },
    {
      "userId": "thanks",
      "jobTitleName": "Program Directory",
      "firstName": "Tom",
      "lastName": "Hanks",
      "preferredFullName": "Tom Hanks",
      "employeeCode": "E3",
      "region": "CA",
      "phoneNumber": "408-2222222",
      "emailAddress": "tomhanks@gmail.com",
      "active" : true
    }
  ]
}

The Object is dynamic (Employees may exists or not )

In case the User Id is nirani can we set the active value to false ?

tried as following

test.find('userId').disabled = 'false';
t.niese
  • 39,256
  • 9
  • 74
  • 101
Pawan
  • 31,545
  • 102
  • 256
  • 434
  • You need to turn that JSON into object and then change the value of cerain key. – Lazar Nikolic Sep 14 '18 at 05:47
  • If you have `var test = { ... }` then the `{ ... }` is not JSON but a JavaScript Object. JSON is a string based representation of data. – t.niese Sep 14 '18 at 05:51
  • 2
    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) – t.niese Sep 14 '18 at 05:54
  • 2
    You have to call `find` on the array stored in `Employees` and you need to use a callback for the `find` method. `test.Employees.find(item => item.userId=='nirani').disabled = 'false';` – t.niese Sep 14 '18 at 05:56

1 Answers1

0

You can use native javascript:

if (test.Employees) {
  for(var i = 0; i < test.Employees.length; i++) {
    var employee = test.Employees[i];
    if (employee.userId === 'nirani') {
      employee.active = false;
    }
  }
}
Bendy Zhang
  • 451
  • 2
  • 10