I'm trying to loop through the data and get it to produce
[
{
odds: "2/3",
position: 1,
terms: "1/5"
},
{
odds: "4/1",
position: 1,
terms: "1/7"
}
]
<script>
var res = '{"count":"2","bettype":"double","position[0]":"1","oddsa[0]":"2","oddsb[0]":"3","placeodds[0]":"1/5","position[1]":"1","oddsa[1]":"4","oddsb[1]":"6","placeodds[1]":"1/7"}';
var line = {};
//
var getvals = JSON.parse(res);
var count = parseInt(getvals["count"]);
var i = 0;
const array = [];//new Array();
for(var i = 0;i < count;i++)
{
line["odds"] = getvals["oddsa["+i+"]"]+'/'+getvals["oddsb["+i+"]"];
line["terms"] = getvals["placeodds["+i+"]"];
line["position"] = getvals["position["+i+"]"];
array.push(line);
}
console.log(array);
</script>
However this is the result I get from the above
[{
odds: "4/6",
position: "1",
terms: "1/7"
}, [circular object Object]]
I'm not a JS coder so trying to learn as I go however I've looked at a few other examples but I'm still having issues
lineobject into the array twice instead of creating two independent objects and pushing each of them. It's not technically circular here but what this output means is that it's a reference to the same thing already printed before at some point. Essentially you havearray[0] === array[1]at the end.lineand push the same single object into the array twice (changing its values in the middle - doesn't make it a copy of the object though!). You need to create a separatelineobject for each iteration.