0

I have my js object/array

[Containerbox-1: Array(2)
0: "textbox-3"
1: "rediobox-4"
length: 2
__proto__: Array(0)
length: 0
__proto__: Array(0)]

Who to convert this in to a string I have used the JSON.stringify(); but i get only [] I am not sure what is the problem here.

I have defined my variable public formFieldParent = []; like this in my component.ts. And further I push elements in it dynamically

if (isNullOrUndefined(this.formFieldParent[targetId])) { 
        this.formFieldParent[targetId] = []; <--- Adding key here 
      }
this.formFieldParent[targetId].push(idNm); <-- adding values here for key

when I console it then the output is as shown above, and returns [] by using JSON.stringify(this.formFieldParent);

what should I use to get the string of it. Here is the screen shot of console enter image description here

Parag Chaure
  • 2,883
  • 2
  • 19
  • 27
  • Youre Close. Containerbox-1 looks like a property on your object, So you might want to do something like: `JSON.stringify(this.formFieldParent['Containerbox-1']);` – Fallenreaper Jan 29 '19 at 18:55
  • key is dynamically added to variable and entire array is important for parent child relation – Parag Chaure Jan 29 '19 at 19:00

1 Answers1

0

I have defined my variable public formFieldParent = [];

That's the problem. You should have used an object here, as you assign textual properties (like Containerbox-1) to it. The array stays empty (length is 0), and that's what will be shown in the JSON. Don't abuse arrays as objects!

Bergi
  • 630,263
  • 148
  • 957
  • 1,375