0

My JSON structure looks like this:

{
    "id": 1,
    "name": "Marks, Price and Conn",
    "description": "Newton",
    "installations": [
      {
        "id": "1",
        "name": "ENIM",
        "key": 106789,
      }
    ]
}

And when in my angular page I click on the client I want to add an installation. How to get current client values and then add a new object to subarray?

I tried simple

export class AddInstallationComponent implements OnInit {
  clients: any;
  client = {
    name: '',
    nip: '',
    description: '',

  installation: [{
    name: '',
    key: '',
  }]
 };

  constructor(private clientService: ClientService) { }
  saveInstallation(): void {

    const data = {

      name: this.client.installation['name'],
      key: this.client.installation['key'],

    };

    this.client.installation.push(data)

  }
}

But I don't know how to get values for my current client which I clicked.

3
  • 1
    Here this.client.installation['name'] you use installation as an object. Here this.client.installation.push(data) you use it like an array and... on the json file you have a different name: installations. Commented Jul 16, 2021 at 8:00
  • @StPaulis okay thank you, so if I want to use it as an object how to pass data to it instead of push method? Commented Jul 16, 2021 at 9:33
  • Try read this: medium.com/@zac_heisey/objects-vs-arrays-42601ff79421 ... although you should have in mind that in JavaScript everything is an object: stackoverflow.com/questions/7803262/… Commented Jul 16, 2021 at 10:24

0

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.