-2

I have an array which consists of the string elements something like this: a = ['First Name', 'Last Name', 'Age'] the actually array is quite large so I have given a small example here.

I have another Json object which has few elements already:

jsonschema = {
                'creationDate': date, 
                'schemaVersion': 1.5
            }

I would like to loop through my array and add the elements to my jsonarray something like this:

jsonschema  =   {
                    'creationDate': date, 
                    'schemaVersion': 1.5,
                    'new arraylist':[
                        "First Name",
                        "Last name",
                        "Age"
                    ]
                }

I tried quite a few things mentioned in the StackOverflow but for some reason nothing seems to work for me. Can someone please help me with this?

Here is what I have been trying using the for loop:

for(var o=0; o<a.length; o++)
{
    jsonschema['new arraylist'] =   a[o];   
}

In this case I am getting only the last element of the array.


How can I elements to JsonArray with {}

I have a Json element already something like this

jsonschema  =   {
                        'creationDate': date, 
                        'schemaVersion': 1.5,
}

I would like to add new element and it should appear something like this:

jsonschema  =   {
                        'creationDate': date, 
                        'schemaVersion': 1.5,
                        'newElement':{
                            'value without kex'
                        }
                }

I am able to achieve this but I am not getting the {} how can I add the single value with {}

BATMAN_2008
  • 2,788
  • 3
  • 31
  • 98
  • 3
    Did you try `jsonschema['new arraylist'] = a;` ? – Sajeeb Ahamed Jul 11 '20 at 10:46
  • Yes I did in that case its adding only the last element of the array but remaining are not displayed in the final json. – BATMAN_2008 Jul 11 '20 at 10:48
  • JSON is a *textual notation* for data exchange. [(More here.)](http://stackoverflow.com/a/2904181/157247) If you're dealing with JavaScript source code, and not dealing with a *string*, you're not dealing with JSON. (If that *were* JSON, it would be invalid JSON; property keys must be in double, not single, quotes in JSON.) – T.J. Crowder Jul 11 '20 at 10:50
  • @T. Actually I am doing the following thing before trying to add the elements. `JSON.parse(JSON.stringify(jsonschema))` – BATMAN_2008 Jul 11 '20 at 10:51
  • 1
    @BATMAN_2008 - Try doing what Sajeeb actually suggested: `jsonschema['new arraylist'] = a;` Your `for` loop is assigning `a[o]` repeatedly, overwriting the previous value you assigned. – T.J. Crowder Jul 11 '20 at 10:51
  • `JSON.parse(JSON.stringify(x))` is almost always an anti-pattern. – T.J. Crowder Jul 11 '20 at 10:51
  • @T.J.Crowder The direct assignment of the array worked for me. I am so stupid that I tried everything and forgot to try the simplest thing. Thanks a lot man :) – BATMAN_2008 Jul 11 '20 at 10:53
  • @BATMAN_2008 - Hey, we've all done it! :-) Happy coding! – T.J. Crowder Jul 11 '20 at 11:05
  • @T.J.Crowder I have one more question, If I want to add an element to the JSONARRAY with the `{}` then how can I achieve it? I have modified the code little bit above can you please have look at provide resolution? – BATMAN_2008 Jul 11 '20 at 12:28
  • @BATMAN_2008 - (Again, none of this is JSON.) `{}` is an object literal. Within an object literal, you either provide properties or methods. Properties need both names and values. If you just want a list of values, use an array instead (`[]`). – T.J. Crowder Jul 11 '20 at 12:37

1 Answers1

0

Try this

aray=['First Name', 'Last Name', 'Age'] 
jsonschema = { 'creationDate': "date", 'schemaVersion': 1.5 }
jsonschema['new arraylist']=[]
aray.forEach(o=>jsonschema['new arraylist'].push(o))
console.log(jsonschema)
Sven.hig
  • 4,449
  • 2
  • 8
  • 18