0

I have a json array with:

[
  {
    "id":"1",
    "0":"1",
    "name":"Quique",
    "1":"Quique"
  },
  {
    "id":"2",
    "0":"2",
    "name":"Kety",
    "1":"Kety"
  }
]

So, I want to get values id and name from the json and use them to represent the json file in with HTML. A perfect result for me will be something like:

<div id="container">
  <div>Quique, id 1</div>
  <div>Kety, id 2</div>
</div>
3
  • 3
    What have you tried so far? Is there a specific error message you're getting? Commented Feb 14, 2017 at 21:30
  • I've tried with a for each, but I don't know how to get values inside and append them Commented Feb 14, 2017 at 21:31
  • Parse the Json to an array with objects. Then loop through the array and for each object add a div inside the container div and fill this div with the correct information from the object. Commented Feb 14, 2017 at 21:32

4 Answers 4

3

Pure Javascript solution using Array.prototype.forEach() function:

var data = [
  {
    "id":"1",
    "0":"1",
    "name":"Quique",
    "1":"Quique"
  },
  {
    "id":"2",
    "0":"2",
    "name":"Kety",
    "1":"Kety"
  }
],
contentStr = "";

data.forEach(function(o){
    contentStr += "<div>"+ o.name +", id "+ o.id +"</div>";
});
document.getElementById('container').innerHTML = contentStr;
<div id="container"> 
</div>


Another alternative would be using Array.prototype.reduce() function:

document.getElementById('container').innerHTML = data.reduce(function(h, o){
  return h + "<div>"+ o.name +", id "+ o.id +"</div>";
}, '');
Sign up to request clarification or add additional context in comments.

2 Comments

Skip the temp string using reduce: document.getElementById('container').innerHTML = data.reduce( (html, item) => html + `<div>${item.name}, id ${item.id}</div>`, '' )
@pawel, added as alternative
2

jQuery solution.

var arr = [{
  "id": "1",
  "0": "1",
  "name": "Quique",
  "1": "Quique"
}, {
  "id": "2",
  "0": "2",
  "name": "Kety",
  "1": "Kety"
}];

(function(arr) {
  return arr.forEach(v => $('#container').append('<div>' + v.name + ', ' + 'id: ' + v.id + '</div>'));
})(arr);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container">

</div>


Pure JS.

var arr = [{
  "id": "1",
  "0": "1",
  "name": "Quique",
  "1": "Quique"
}, {
  "id": "2",
  "0": "2",
  "name": "Kety",
  "1": "Kety"
}];

(function(arr) {
  for(var i = 0; i < arr.length; i++) {
  var node = document.createElement("div");  
  var textnode = document.createTextNode(arr[i].name + ', id: ' + arr[i].id); 
  node.appendChild(textnode);    
  document.getElementById("container").appendChild(node); 
}
})(arr);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container">

</div>

1 Comment

Modifying the DOM inside a loop is not the best approach when working with large sets of data. You should build a String along the way, and when the loop is done, insert it in the DOM.
0

var arr = [
  {
    "id":"1",
    "0":"1",
    "name":"Quique",
    "1":"Quique"
  },
  {
    "id":"2",
    "0":"2",
    "name":"Kety",
    "1":"Kety"
  }
];

function div(o) {
  var d = document.createElement('div');
  d.textContent = o.name + " id " + o.id;
  return d;
}

function generate(arr) {
  var wrapper = document.getElementById("container");
  arr.forEach(function(o) {
    wrapper.appendChild(div(o));
  });
}

generate(arr);
<div id="container"></div>

Comments

0

Here's a quick example.

var data = [
  {
    "id":"1",
    "0":"1",
    "name":"Quique",
    "1":"Quique"
  },
  {
    "id":"2",
    "0":"2",
    "name":"Kety",
    "1":"Kety"
  }
]

var root = document.getElementById('container');

for( var key in data ){
  element = '<div>' + data[key].name + ', id: ' + data[key].id + '</div>';
  root.innerHTML += element;
}
<div id="container"></div>

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.