1

I have a JSON in this format:

JSON No 1

{
  "status": "ok",
  "data": [{
    "id": 1,
    "name": "building No1",
    "floor": 5,
    "code": {
      "1": [{
        "id": 1,
        "code": "MCD-001",
        "selected": false
      }, {
        "id": 2,
        "code": "MCD-002",
        "selected": false
      }],
      "2": [{
        "id": 3,
        "code": "MCD-003",
        "selected": false
      }],
      "3": [{
        "id": 4,
        "code": "MCD-004",
        "selected": false
      }, {
        "id": 5,
        "code": "MCD-004-bis",
        "selected": false
      }, {
        "id": 6,
        "code": "MCD-005",
        "selected": false
      }],
      "4": [{
        "id": 7,
        "code": "MCD-006",
        "selected": false
      }],
      "6": [{
        "id": 8,
        "code": "MCD-007",
        "selected": false
      }],
      "7": [{
        "id": 9,
        "code": "MCD-008",
        "selected": false
      }, {
        "id": 10,
        "code": "MCD-009",
        "selected": false
      }]
    },
    "building_name": "Test Tower",
    "number_lot": true,
    "parking_floor": 0,
    "creation_date": {
      "date": "2017-01-30 00:00:00.000000",
      "timezone_type": 3,
      "timezone": "UTC"
    }
  }]
}

I have another JSON, something like this:

JSON No 2

{
  "LOTS": {
    "1": [{
      "id": 1,
      "code": "LOT-001",
      "floor": 1,
      "selected": true
    }, {
      "id": 2,
      "code": "MCD-002",
      "floor": 1,
      "selected": true
    }],
    "7": [{
      "id": 9,
      "code": "MCD-008",
      "floor": 7,
      "selected": true
    }, {
      "id": 10,
      "code": "MCD-009",
      "floor": 7,
      "selected": true
    }]
  }
}

Now what I want to set "selected":true in the JSON 1 key where code is MCD-008 and id:9

Does AngularJS provide someway to do either at template or controller end?

All I want to set button's classes to "default" or "primary". This thing is already being done. All I need to pass selected:true

8
  • You might need to loop, find and update Commented Feb 17, 2017 at 11:41
  • 1
    You need to loop through the arrays and match the keys in plain javascript. No build in feature in angularjs that handles such custom situation. Commented Feb 17, 2017 at 11:41
  • Your JSON No 2 is not a valid json. It should be {"lots":[{"id":9,"code":"MCD-008","selected":true}]}. Also, it is true that there is no built in method to handle this thing. Commented Feb 17, 2017 at 11:44
  • @AnadiSharma just updated Question Commented Feb 17, 2017 at 11:46
  • Question is too broad without knowing how you use this data in the app Commented Feb 17, 2017 at 11:49

1 Answer 1

1

You could use a hash table and het first the id and code from the source object and the interate the replacment object and set the values.

var data1 = { status: "ok", data: [{ id: 1, name: "building No1", floor: 5, code: { "1": [{ id: 1, code: "MCD-001", selected: false }, { id: 2, code: "MCD-002", selected: false }], "2": [{ id: 3, code: "MCD-003", selected: false }], "3": [{ id: 4, code: "MCD-004", selected: false }, { id: 5, code: "MCD-004-bis", selected: false }, { id: 6, code: "MCD-005", selected: false }], "4": [{ id: 7, code: "MCD-006", selected: false }], "6": [{ id: 8, code: "MCD-007", selected: false }], "7": [{ id: 9, code: "MCD-008", selected: false }, { id: 10, code: "MCD-009", selected: false }] }, building_name: "Test Tower", number_lot: true, parking_floor: 0, creation_date: { date: "2017-01-30 00:00:00.000000", timezone_type: 3, timezone: "UTC" } }] },
    data2 = { LOTS: { "1": [{ id: 1, code: "LOT-001", floor: 1, selected: true }, { id: 2, code: "MCD-002", floor: 1, selected: true }], "7": [{ id: 9, code: "MCD-008", floor: 7, selected: true }, { id: 10, code: "MCD-009", floor: 7, selected: true }] } },
    hash = Object.create(null);

data1.data.forEach(function (o) {
    Object.keys(o.code).forEach(function (k) {
        o.code[k].forEach(function (a) {
            var key = [a.id, a.code].join('|');
            hash[key] = a;
        });
    });
});

Object.keys(data2.LOTS).forEach(function (k) {
    data2.LOTS[k].forEach(function (a) {
        var key = [a.id, a.code].join('|');
        if (hash[key]) {
            hash[key].selected = a.selected;
        }
    });
});

console.log(data1);
.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.