0

JSON data is something in this format.

[{
    "_id": "5b706faf2605576fefb9ae9c",
    "index": 0,
    "guid": "46850469-5924-4966-b7d6-92125444cf98",
    "isActive": true,
    "balance": "$2,983.78",
    "friends": [{
            "fid": 0,
            "fname": "Sondra Fields"
        },
        {
            "fid": 1,
            "fname": "Sondra"
        },
        {
            "fid": 2,
            "fname": "Fields"
        }
    ],
    "greeting": "Hello,",
    "favoriteFruit": "apple"
}]

I am trying to get friends "fid" and "fname" but having the hard time doing so. I am doing something like this.

var dat = { array: data }
var actual = dat.array.reduce((p, n, index) => p.concat(n[index - 1]))

initially data is in "data" variable. by doing this console.log((actual.friends)); I got the following result.

  [ { fid: 0, fname: 'Sondra Fields' },
  { fid: 1, fname: 'Sondra' },
  { fid: 2, fname: 'Fields' } ]

Unable to further extract the data. Thank you

Sookie Singh
  • 1,543
  • 11
  • 17
Adeel Tahir
  • 187
  • 2
  • 10
  • json is not encapsulated in brackets – Darkrum Aug 12 '18 at 19:25
  • 1
    JSON is a string. Until you parse that string, you'll only be able to use string functions to deal with it. If `data` is the result of calling `JSON.parse()` on a string, then it is no longer JSON and is in fact a JavaScript object (an array in your case). – Heretic Monkey Aug 12 '18 at 19:39
  • Possible duplicate of [Access / process (nested) objects, arrays or JSON](https://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Heretic Monkey Aug 12 '18 at 19:39
  • It’s unclear what your actual problem is. Please provide actual runnable code (including data), ideally as a jsfiddle or runnable code snippet. See https://stackoverflow.com/help/mcve – jcaron Aug 12 '18 at 22:53

1 Answers1

0

By Iterating over actual.friends you can get the value of fid and fname.

data=[
{
    "_id": "5b706faf2605576fefb9ae9c",
    "index": 0,
    "guid": "46850469-5924-4966-b7d6-92125444cf98",
    "isActive": true,
    "balance": "$2,983.78",
    "friends": [
        {
            "fid": 0,
            "fname": "Sondra Fields"
        },
        {
            "fid": 1,
            "fname": "Sondra"
        },
        {
            "fid": 2,
            "fname": "Fields"
        }
],
    "greeting": "Hello,",
    "favoriteFruit": "apple"
  }
]

var dat = { array: data }
var actual = dat.array.reduce((p, n, index) => p.concat(n[index - 1]))
actual.friends.forEach(function(element) {
  console.log(element.fid,element.fname);
});

or

data.forEach(function(ele) {
ele.friends.forEach(function(element) {
  console.log(element.fid,element.fname);
  });
});

data=[
{
    "_id": "5b706faf2605576fefb9ae9c",
    "index": 0,
    "guid": "46850469-5924-4966-b7d6-92125444cf98",
    "isActive": true,
    "balance": "$2,983.78",
    "friends": [
        {
            "fid": 0,
            "fname": "Sondra Fields"
        },
        {
            "fid": 1,
            "fname": "Sondra"
        },
        {
            "fid": 2,
            "fname": "Fields"
        }
],
    "greeting": "Hello,",
    "favoriteFruit": "apple"
  }
]

data.forEach(function(ele) {
ele.friends.forEach(function(element) {
  console.log(element.fid,element.fname);
  });
});
NullPointer
  • 7,094
  • 5
  • 27
  • 41