sorry I feel this is sort of a stupid question I have searched quite a bit and I cannot find my answer.
I have an array in JavaScript that I am inserting a bunch of elements. The trouble is I cannot then get the values of the elements back out, I think I am getting the property of the actual array. Below is what I am doing and what I am trying to do. Thanks in advance for the help. This is not the full traverse method, it actually does go through a bunch of times. When I previously just had an associative array items[a] it worked perfectly.
var element = { html: "", group: "" };
var array = [];
function traverse(o) {
for (var key in o) {
element.html = "<li class='item'>" + key + " : </li>";
element.group = b;
array.push(element);
}
}
I want to print the html from each element, the issue is I get the .html from only the first element each time.
function printElements() {
for (item in array) {
var element = array.pop();
console.log(element.html);
}
}
I also tried.
function printElements() {
for (item in array) {
console.log(array.html);
}
}
I want to keep the array intact, I do not want to pop and elements I just want to print them.
for-inon arrays can cause bugs in JavaScript.