0

I know this has been asked before but I can't seem to find the answer. I just want to know how to access the values in service_name array in a console.log

Below is the array and just a snip out of the data.

var a = [{"ID":102254,"Name":"obj 1","service_name":["Open  Ticket","Escalation Ticket"]},{"ID":226619,"Name":"DIDE","service_name":["Open  Ticket","Escalation Ticket"]},{"ID":224522,"Name":"CAT","service_name":["Open  Ticket","Escalation Ticket"]},{"ID":122533,"Name":"Mirror","service_name":["Open  Ticket","Escalation Ticket"]}]


console.log(JSON.stringify(a, 0, 4))
user3292394
  • 609
  • 2
  • 11
  • 24
  • Well, you have an array of objects, each object containing a `service_name` array. Do you just want a single one, or do you want an array of the arrays, etc.? What would your desired final result look like? – mhodges Mar 30 '17 at 21:11
  • I will be filtering down the above array to just one ID. and then will want to access just the service_name info for that ID – user3292394 Mar 30 '17 at 21:13
  • Answer posted. Let me know if that's what you were looking for – mhodges Mar 30 '17 at 21:21
  • Just marked, an answer, thanks for all the responses, they all do what I was looking for, but the one I marked suits my needs best. Many thanks – user3292394 Mar 30 '17 at 21:26
  • Possible duplicate of [Access / process (nested) objects, arrays or JSON](https://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Heretic Monkey Feb 28 '19 at 18:17

5 Answers5

1

you can access it like an array, this is how you would access the service_name values in the 4th item in the array

var a = [{"ID":102254,"Name":"obj 1","service_name":["Open  Ticket","Escalation Ticket"]},{"ID":226619,"Name":"DIDE","service_name":["Open  Ticket","Escalation Ticket"]},{"ID":224522,"Name":"CAT","service_name":["Open  Ticket","Escalation Ticket"]},{"ID":122533,"Name":"Mirror","service_name":["Open  Ticket","Escalation Ticket"]}]


console.log(JSON.stringify(a[3].service_name[0]))
console.log(JSON.stringify(a[3].service_name[1]))
Jpsh
  • 1,697
  • 12
  • 17
0

Just do:

var a = [{"ID":102254,"Name":"obj 1","service_name":["Open  Ticket","Escalation Ticket"]},{"ID":226619,"Name":"DIDE","service_name":["Open  Ticket","Escalation Ticket"]},{"ID":224522,"Name":"CAT","service_name":["Open  Ticket","Escalation Ticket"]},{"ID":122533,"Name":"Mirror","service_name":["Open  Ticket","Escalation Ticket"]}]
a.forEach((e)=>console.log(e.service_name));

if you need a result array:

var a = [{"ID":102254,"Name":"obj 1","service_name":["Open  Ticket","Escalation Ticket"]},{"ID":226619,"Name":"DIDE","service_name":["Open  Ticket","Escalation Ticket"]},{"ID":224522,"Name":"CAT","service_name":["Open  Ticket","Escalation Ticket"]},{"ID":122533,"Name":"Mirror","service_name":["Open  Ticket","Escalation Ticket"]}]
var res = a.map((e)=>e.service_name);
console.log(res);
Nelson Teixeira
  • 6,297
  • 5
  • 36
  • 73
0

var a = [{"ID":102254,"Name":"obj 1","service_name":["Open  Ticket","Escalation Ticket"]},{"ID":226619,"Name":"DIDE","service_name":["Open  Ticket","Escalation Ticket"]},{"ID":224522,"Name":"CAT","service_name":["Open  Ticket","Escalation Ticket"]},{"ID":122533,"Name":"Mirror","service_name":["Open  Ticket","Escalation Ticket"]}]

a.forEach(function(element) {
  console.log(element.service_name);
});
meda
  • 45,103
  • 14
  • 92
  • 122
0

You can create a function to return the service names array of the element whose ID matches the id you are looking for like so:

function getServiceNamesById (data, id) {
  for (var i = 0; i < data.length; i++) {
    if (data[i].ID === id) {
      return data[i].service_name;
    }
  }
  return [];
}

var a = [{"ID":102254,"Name":"obj 1","service_name":["Open  Ticket","Escalation Ticket"]},{"ID":226619,"Name":"DIDE","service_name":["Open  Ticket","Escalation Ticket"]},{"ID":224522,"Name":"CAT","service_name":["Open  Ticket","Escalation Ticket"]},{"ID":122533,"Name":"Mirror","service_name":["Open  Ticket","Escalation Ticket"]}];

var id = 226619;

console.log(getServiceNamesById(a, id));
mhodges
  • 10,938
  • 2
  • 28
  • 46
0

Try this one :

  var a = [{"ID":102254,"Name":"obj 1","service_name":["Open  Ticket","Escalation Ticket"]},{"ID":226619,"Name":"DIDE","service_name":["Open  Ticket","Escalation Ticket"]},{"ID":224522,"Name":"CAT","service_name":["Open  Ticket","Escalation Ticket"]},{"ID":122533,"Name":"Mirror","service_name":["Open  Ticket","Escalation Ticket"]}]

 a.forEach(function(item){ // item is an object
     item.service_name.forEach(function(service){ //item.service_name is  an array
     console.log(service);
     });
 });

Good luck

kourouma_coder
  • 1,078
  • 2
  • 13
  • 24