ismsm
ismsm

Reputation: 173

How to loop on a JSON data and put part of it in another array?

I want to loop on a JSON data and put a part of it in a sperate array.

I have this JSON data:

{"msg":"[{"parent":"0","child":"1"},{"parent":"0","child":"2"}]","sig":"3045022100b3f03c83ed1f3d0bc72475550f23c0dd38277b0e2230e1fe76611577b2ca4b7e022037c77d37cc08ed897d0d1a11d09342e6abb73e641d20d5d55f367044d5489ddf"}

I want to make an array of msg only like this:

[ '0', '1', '0', '2 ]

My code:

var message = '[{"msg":[{"parent":"0","child":"1"},{"parent":"0","child":"2"}],"sig":"3045022100b3f03c83ed1f3d0bc72475550f23c0dd38277b0e2230e1fe76611577b2ca4b7e022037c77d37cc08ed897d0d1a11d09342e6abb73e641d20d5d55f367044d5489ddf"}]'
var g = [];
const jsArray = JSON.parse(message);
Array.from(jsArray.msg).forEach(jsdata => {
  console.log(jsdata)
  g.push(jsdata.parent, jsdata.child)
})
console.log(g);

I could not meet what I really want it says:

[
  undefined, undefined, undefined, undefined, undefined, undefined,
  undefined, undefined, undefined, undefined, undefined, undefined,
  undefined, undefined, undefined, undefined, undefined, undefined,
  undefined, undefined, undefined, undefined, undefined, undefined,
  undefined, undefined, undefined, undefined, undefined, undefined,
  undefined, undefined, undefined, undefined, undefined, undefined,
  undefined, undefined, undefined, undefined, undefined, undefined,
  undefined, undefined, undefined, undefined, undefined, undefined,
  undefined, undefined, undefined, undefined, undefined, undefined,
  undefined, undefined, undefined, undefined, undefined, undefined,
  undefined, undefined, undefined, undefined, undefined, undefined,
  undefined, undefined, undefined, undefined, undefined, undefined,
  undefined, undefined, undefined, undefined, undefined, undefined,
  undefined, undefined, undefined, undefined, undefined, undefined,
  undefined, undefined, undefined, undefined, undefined, undefined,
  undefined, undefined, undefined, undefined, undefined, undefined,
  undefined, undefined, undefined, undefined,
  ... 10 more items
]

How could I solve this, please? Thanks in advance

Upvotes: 0

Views: 116

Answers (3)

Omle
Omle

Reputation: 51

For working with JSON as a JS you will have to use JSON.parse(). This will spread your JSON object to an Array instance with its attribute . Then you can loop over the array and do whatever you like. When you are done with the instance and you want to pass it to a function , use JSON.stringify() to convert the Object to a string (which you can write or pass)

Upvotes: 0

Beshambher Chaukhwan
Beshambher Chaukhwan

Reputation: 1448

Try the code below to loop the inner array:

    var message ='[{"msg":[{"parent":"0","child":"1"},{"parent":"0","child":"2"}],"sig":"3045022100b3f03c83ed1f3d0bc72475550f23c0dd38277b0e2230e1fe76611577b2ca4b7e022037c77d37cc08ed897d0d1a11d09342e6abb73e641d20d5d55f367044d5489ddf"}]';
    let g=[];
    const jsArray = JSON.parse(message);
    jsArray.forEach(jsMsgData => {
         // console.log(jsMsgData.msg);
         jsMsgData.msg.forEach(jsdata => {
            g.push(jsdata.parent, jsdata.child);
         })
    });
    console.log(g);

check full code here: https://ideone.com/QywYfb

Upvotes: 1

Randy Casburn
Randy Casburn

Reputation: 14165

It is as simple as using the .map method to gather the data and then flatting the final array to get you output.

var message ='[{"msg":[{"parent":"0","child":"1"},{"parent":"0","child":"2"}],"sig":"3045022100b3f03c83ed1f3d0bc72475550f23c0dd38277b0e2230e1fe76611577b2ca4b7e022037c77d37cc08ed897d0d1a11d09342e6abb73e641d20d5d55f367044d5489ddf"}]';

let data = JSON.parse(message);
let output = data[0].msg.map(d=>[d.parent,d.child]).flat();
console.log(output);

Upvotes: 2

Related Questions