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))

5
  • 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? Commented Mar 30, 2017 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 Commented Mar 30, 2017 at 21:13
  • Answer posted. Let me know if that's what you were looking for Commented Mar 30, 2017 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 Commented Mar 30, 2017 at 21:26
  • Possible duplicate of Access / process (nested) objects, arrays or JSON Commented Feb 28, 2019 at 18:17

5 Answers 5

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]))

Sign up to request clarification or add additional context in comments.

1 Comment

This does exactly what i needed
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);
});

Comments

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);

Comments

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));

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.