0

I have this json file that I am appending to the html.

"{\"Item1\":[{\"Id\":2,\"Title\":\"Support\",\"Items\":8},{\"Id\":5,\"Title\":\"Datacenter\",\"Items\":5},{\"Id\":3,\"Title\":\"Bogholderiet\",\"Items\":5},{\"Id\":8,\"Title\":\"Helpdesk\",\"Items\":4},{\"Id\":9,\"Title\":\"SLA og VIP\",\"Items\":1},{\"Id\":7,\"Title\":\"Hostmaster\",\"Items\":1}],\"Item2\":[{\"Id\":7,\"FullName\":\"p11\",\"Items\":5},{\"Id\":17,\"FullName\":\"p8\",\"Items\":3},{\"Id\":9,\"FullName\":\"p10\",\"Items\":3},{\"Id\":8,\"FullName\":\"p6\",\"Items\":3},{\"Id\":3,\"FullName\":\"p1\",\"Items\":3},{\"Id\":5,\"FullName\":\"p2\",\"Items\":2},{\"Id\":16,\"FullName\":\"p3\",\"Items\":1},{\"Id\":11,\"FullName\":\"p4\",\"Items\":1}]}"

My question is how can I append only the value of "SLA og VIP". Below code I am able to access to array of Item1 and appending it to html.

if (key === 'Item1') {
value[key].forEach(function (val) {
var tbl3Row = "<tr " + (parseInt(val.TotalUnresolvedItems) > 3 ? " class='colorgul'" : "") + (parseInt(val.TotalUnresolvedItems) < 4 ? " class='colorgreen'" : "") + ">" + "<td>" + val.Title + "</td>" + "<td>" + val.TotalUnresolvedItems + "</td>" + "</tr>"
table3Rows += tbl3Row;                                   
 })
 }

But now I am only trying to access to the Title value"SLA og VIP". This is the formula that i manage to find:

parsedData.Item1[4]

I just dont know how can I use this inside of this code:

if (key === 'Item1') {
value[key].forEach(function (val) {
var tbl3Row = "<tr " + (parseInt(val.TotalUnresolvedItems) > 3 ? " class='colorgul'" : "") + (parseInt(val.TotalUnresolvedItems) < 4 ? " class='colorgreen'" : "") + ">" + "<td>" + val.Title + "</td>" + "<td>" + val.TotalUnresolvedItems + "</td>" + "</tr>"
table3Rows += tbl3Row;                                   
 })
 }
3
  • Not clear, be more precise Commented Apr 4, 2017 at 12:21
  • JSON.parse(string).Item1.find(x=>x.Id === 9).Title Commented Apr 4, 2017 at 12:21
  • You currently have 2 objects, each holding an array of Objects, each having Id, Title or Fullname and Items properties. What are you calling the value of "SLA og VIP"? What are the conditions on which you want to return something? You could use the id===9 and having a Title property. Is this the only possible JSON or are there any cases? What's the logic behind it? Commented Apr 4, 2017 at 12:35

1 Answer 1

2

You can [].prototype.filter() it:

Because it will let you filter to the exact object you need to target. In your case it is

{
  "Id": 9,
  "Title": "SLA og VIP",
  "Items": 1
}

With this you can get the filtered object's properties like Id, Title, Items.

if (key === 'Item1') {
  var obj = value[key].filter(function(item){
     return item.Title === "SLA og VIP"
  })[0];

  // now use obj.Title, output: "SLA og VIP"

  value[key].forEach(function(val) {
    var tbl3Row = "<tr " + (parseInt(val.TotalUnresolvedItems) > 3 ? "...</tr>"
    table3Rows += tbl3Row;
  })
}

var json = "{\"Item1\":[{\"Id\":2,\"Title\":\"Support\",\"Items\":8},{\"Id\":5,\"Title\":\"Datacenter\",\"Items\":5},{\"Id\":3,\"Title\":\"Bogholderiet\",\"Items\":5},{\"Id\":8,\"Title\":\"Helpdesk\",\"Items\":4},{\"Id\":9,\"Title\":\"SLA og VIP\",\"Items\":1},{\"Id\":7,\"Title\":\"Hostmaster\",\"Items\":1}],\"Item2\":[{\"Id\":7,\"FullName\":\"p11\",\"Items\":5},{\"Id\":17,\"FullName\":\"p8\",\"Items\":3},{\"Id\":9,\"FullName\":\"p10\",\"Items\":3},{\"Id\":8,\"FullName\":\"p6\",\"Items\":3},{\"Id\":3,\"FullName\":\"p1\",\"Items\":3},{\"Id\":5,\"FullName\":\"p2\",\"Items\":2},{\"Id\":16,\"FullName\":\"p3\",\"Items\":1},{\"Id\":11,\"FullName\":\"p4\",\"Items\":1}]}";

var obj = JSON.parse(json)["Item1"].filter(function(item) {
  return item.Title === "SLA og VIP"
})[0];

console.log(obj);

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

3 Comments

@AndreiGheorghiu IMHO you might be right but this answer is just answers the precisely asked question. Although i would add the link and some description.
I'll delete that comment, since it no longer applies. Thank you.
@Jai Thank you.I think I have understood that with .filter(function(item){ return item.Title === "*" })[0]; I am able to filter specific array in an objects.I can also see it in the console when I write console.log(obj);. But when I use value[key].forEach(function(val) { var tbl3Row = "<tr " + (parseInt(val.TotalUnresolvedItems) > 3 ? "...</tr>" table3Rows += tbl3Row; It appends all the arrays in objects. Do you have any Ide of why. When I change value[key].forEach(function(val) to value[obj].forEach(function(val) I do get an error says that it is not a valid function.

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.