Changed it to

##########

'use strict'

var str='{ \

        "a": ["x", "y"], \

        "rest": "fdsf", \

        "ff": "ll" \

}'


var jsonstr=JSON.parse(str);


var payload=[];


if (Array.isArray(jsonstr['a'])){


        console.log("Is array");


        var payload=[];


        jsonstr['a'].forEach(function(item, index) {


                console.log(item);

                var jsontemp=jsonstr;


                jsontemp.a=item;


                console.log(jsontemp);


                payload.push(jsontemp);



        });


}


console.log(payload);


####################


i get the same result.

On Wednesday, 3 August 2016 14:16:31 UTC-7, jfhbrook wrote:
>
> It looks like jsontemp is a global variable here, due to the lack of a 
> 'var', 'let' or similar. 
>
> fwiw adding 'use strict'; at the top of your file should make this 
> complain rather than silently doing what you don't want. 
>
> --Josh 
>
> On Wed, Aug 3, 2016 at 3:12 PM, Tippur <cti...@gmail.com <javascript:>> 
> wrote: 
> > I have a json object that has an array field 
> > 
> > I want to take that object and break it into individual json objects. 
> > 
> > For example { a:[ 'x', 'y'], rest: 'fdsf', ff: 'll' } should be broken 
> into 
> > 
> > { a: 'x', rest: 'fdsf', ff: 'll' } and { a: 'y', rest: 'fdsf', ff: 'll' 
> } 
> > 
> > 
> > Here is my code: 
> > ################################## 
> > 
> > var str='{ \ 
> > 
> >         "a": ["x", "y"], \ 
> > 
> >         "rest": "fdsf", \ 
> > 
> >         "ff": "ll" \ 
> > 
> > }' 
> > 
> > jsonstr=JSON.parse(str); 
> > 
> > var payload=[]; 
> > 
> > if (Array.isArray(jsonstr['a'])){ 
> > 
> >         console.log("Is array"); 
> > 
> >         var payload=[]; 
> > 
> >         jsonstr['a'].forEach(function(item, index) { 
> > 
> >                 console.log(item); 
> > 
> >                 jsontemp=jsonstr; 
> > 
> >                 jsontemp.a=item; 
> > 
> >                 console.log(jsontemp); 
> > 
> >                 payload.push(jsontemp); 
> > 
> >         }); 
> > 
> > } 
> > 
> > console.log(payload); 
> > 
> > ################################## 
> > 
> > Here is the output 
> > 
> > [ { a: 'y', rest: 'fdsf', ff: 'll' }, 
> > 
> >   { a: 'y', rest: 'fdsf', ff: 'll' } ] 
> > 
> > 
> > As you can see, the object appended to the final payload has the same 
> > element (payload.a) 
> > 
> > 
> > What am I doing wrong? 
> > 
> > 
> > - Shekar 
> > 
> > 
> > 
> > 
> > 
> > 
> > -- 
> > Job board: http://jobs.nodejs.org/ 
> > New group rules: 
> > https://gist.github.com/othiym23/9886289#file-moderation-policy-md 
> > Old group rules: 
> > https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines 
> > --- 
> > You received this message because you are subscribed to the Google 
> Groups 
> > "nodejs" group. 
> > To unsubscribe from this group and stop receiving emails from it, send 
> an 
> > email to nodejs+un...@googlegroups.com <javascript:>. 
> > To post to this group, send email to nod...@googlegroups.com 
> <javascript:>. 
> > To view this discussion on the web visit 
> > 
> https://groups.google.com/d/msgid/nodejs/8275a391-bc72-4bba-a43f-a17be42f0d61%40googlegroups.com.
>  
>
> > For more options, visit https://groups.google.com/d/optout. 
>

-- 
Job board: http://jobs.nodejs.org/
New group rules: 
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to nodejs+unsubscr...@googlegroups.com.
To post to this group, send email to nodejs@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/0c07b08f-3f2f-49e4-b268-179fb840ea64%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to