0

I am getting a json object just like below

 response = [
{
  'a': [
    {
      'b': [
        {
          'c': [
            {
              'name': 'abc',
              'value': 900
            }
          ]
        }
      ]
    }
  ]
},
{
  'a': [
    {
      'b': [
        {
          'c': [
            {
              'name': 'abc',
              'amount': 900
            }
          ]
        }
      ]
    }
  ]
}
];

now I am looping over the object using the code below

this.response.forEach(
    (event) => {
      event.a.forEach(
          () => {

          }
      );
    }
)

and while compiling it I am getting an error message

error TS2349: Cannot invoke an expression whose type lacks a call signature. Type '((callbackfn: (value: { 'b': { 'c': { 'name': string; 'value': number; }[]; }[]; }, index: number...' has no compatible call signatures.

Any fix for the above error?. Thanks in advance

3
  • stackoverflow.com/questions/39691889/… Commented Sep 25, 2018 at 12:35
  • What is giving you that response? Commented Sep 25, 2018 at 12:35
  • Maybe because you're using a parameterless lambda () => { ... } in the inner forEach. Using (b) => { ... } might solve the problem. Commented Sep 25, 2018 at 12:37

2 Answers 2

3

This is the way to access c.name and the c.amount

response.forEach(element => {
      element['a'].forEach(a => {
           a['b'].forEach(b => {
               b['c'].forEach(c => {
                     console.log(c.name);
                     console.log(c.amount);
               });
            });
      });
 });

JSFiddle => https://jsfiddle.net/jpwga2du/

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

Comments

1
this.response.forEach((el1: any) => {
  el1.a.forEach((el2: any) => {
    el2.b.forEach((el3: any) => {
      el3.c.forEach(el4 => {
        console.log(el4)
      });
    });
  });
});

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.