Parsing Xml With Javascript And Create Array
I am stepping through a bunch of XML, trying to build an array within javascript. XML: 12
Solution 1:
I think it is the .each
function that is causing the issue, you should replace it with simple for
loop
var items = jQuery(details).find("item");
for (var i = 0; i < items.length; i++) {
var ret = [];
ret[0] = jQuery(items[i]).find('prodcode').text();
console.log("parse " + ret[0]);
ret[1] = jQuery(items[i]).find('qty').text();
ret[2] = jQuery(items[i]).find('tax').text();
ret[3] = jQuery(items[i]).find('uprice').text();
ret[4] = jQuery(items[i]).find('price').text();
tot.push(ret);
console.log("tot=" + tot);
}
Solution 2:
You are pushing a reference to the same ret array multiple times to tot, then changing the referenced ret array.
Also note that tot.push(ret) does not push each item from ret onto tot, rather it pushes a reference to ret to tot. When you change the contents of ret in the next round, it will look like every item in tot is changing, because they are all references to the same array.
Perhaps instead of tot.push(ret) you want to
tot.push(ret[0], ret[1], ret[2], ret[3], ret[4]);
Or don't make that ret array at all and instead just push individual texts as you get them.
Post a Comment for "Parsing Xml With Javascript And Create Array"