Ris
Ris

Reputation: 1164

Iterate nested object structure for specific element

I have following nested object. What would be the easiest way to get all "scope" values in an array without referring them individually like,

JSONObject["inc"]["scope"]

Object:

[{
    "inc": [{
        "type": "Entity",
        "scope": "A"
    }, {
        "type": "Entity",
        "scope": "B"
    }],
    "inv": [{
        "type": "Entity",
        "scope": "C"
    }],
    "oth": [{
        "type": "Entity",
        "scope": "D"
    }],
    "pro": [{
        "type": "Support Dept",
        "scope": "E"
    }, {
        "type": "Entity",
        "scope": "F"
    }, {
        "type": "Entity",
        "scope": "C"
    }],
    "ap": [{
        "type": "Support Dept",
        "scope": "A"
    }]
}]

Would appreciate any help on this.

Upvotes: 0

Views: 49

Answers (3)

trincot
trincot

Reputation: 350272

Here is how you could do it:

var obj = [{
    "inc": [{
        "type": "Entity",
        "scope": "A"
    }, {
        "type": "Entity",
        "scope": "B"
    }],
    "inv": [{
        "type": "Entity",
        "scope": "C"
    }],
    "oth": [{
        "type": "Entity",
        "scope": "D"
    }],
    "pro": [{
        "type": "Support Dept",
        "scope": "E"
    }, {
        "type": "Entity",
        "scope": "F"
    }, {
        "type": "Entity",
        "scope": "C"
    }],
    "ap": [{
        "type": "Support Dept",
        "scope": "A"
    }]
}];
    
res =  Object.keys(obj[0]).reduce(function (acc, key) {
    return acc.concat(obj[0][key].map (function (o) {
        return o.scope;
    }));
}, []);

console.log(res);

Please don't call your variable JSONObject. There is no such thing. JSON is a text format for data exchange. What you have is a JavaScript object, which might have been derived from JSON you parsed, but it is not JSON.

Upvotes: 1

echopeak
echopeak

Reputation: 261

One approach i would take would be the functional approach.

ES2015

let scopes = obj.reduce((start ,item) => {
  start = Object.keys(item).map(e => item[e]);
  return start.concat.apply([] , start).map( e => e.scope);
},[]);
console.log('ES2015',scopes);

ES5

var scopes = obj.reduce(function(start ,item){
  start = Object.keys(item).map(function(e){
    return item[e];
  })
  return start.concat.apply([] , start).map(function(e){
    return e.scope
  });
},[]);

console.log('ES5',scopes)

Upvotes: 1

Ajay Narain Mathur
Ajay Narain Mathur

Reputation: 5466

Try this:

var obj = [{
  "inc": [{
    "type": "Entity",
    "scope": "A"
  }, {
    "type": "Entity",
    "scope": "B"
  }],
  "inv": [{
    "type": "Entity",
    "scope": "C"
  }],
  "oth": [{
    "type": "Entity",
    "scope": "D"
  }],
  "pro": [{
    "type": "Support Dept",
    "scope": "E"
  }, {
    "type": "Entity",
    "scope": "F"
  }, {
    "type": "Entity",
    "scope": "C"
  }],
  "ap": [{
    "type": "Support Dept",
    "scope": "A"
  }]
}];

var arr = [];
for (var i in obj[0]) {
  for (var j in obj[0][i]) {
    arr.push(obj[0][i][j]["scope"])
  }
}
console.log(arr)

Upvotes: 1

Related Questions