0

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

ChrisYates
  • 31
  • 5
  • 1
    The reason you get this output is because you are pushing the same `line` object 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 have `array[0] === array[1]` at the end. – CherryDT Nov 27 '21 at 17:49
  • 1
    See also: [Is JavaScript a pass-by-reference or pass-by-value language?](https://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language) – CherryDT Nov 27 '21 at 17:50
  • I dont even think I need to create an array for what I'm trying to achieve .. I'm just a bit puzzled how to achieve what I'm trying to do – ChrisYates Nov 27 '21 at 17:51
  • No you are right, you do need an array, because you are getting the input in a weird format (probably from a web form) that's just a flat list of things with indices as part of the name, so the approach of creating an array for it yourself is indeed correct. The problem is just that you create a _single_ object `line` and 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 separate `line` object for each iteration. – CherryDT Nov 27 '21 at 17:52

2 Answers2

1

You need to create a new line object every iteration of the loop. Otherwise you are modifying and pushing the same object reference over and over to the array

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 getvals = JSON.parse(res);
var count = parseInt(getvals["count"]);

var i = 0;
const array = []; //new Array();
for (var i = 0; i < count; i++) {
  // create new object here
  var line = {};
  line["odds"] = getvals["oddsa[" + i + "]"] + '/' + getvals["oddsb[" + i + "]"];
  line["terms"] = getvals["placeodds[" + i + "]"];
  line["position"] = getvals["position[" + i + "]"];
  array.push(line);
}
console.log(array);
charlietfl
  • 170,828
  • 13
  • 121
  • 150
0

In the second "what do you want" object you don't get 4/1 with the logic you used because oddssa[1] is 4 and oddsb[1] is 6 so you'll get 4/6

The logic itself looks promising keep it up! But as soon as I specified an object inside loop I got the result you specified.

I also cleaned up, and added the template literals (backticks) so you wouldn't be so confused with the pluses to concat the strings, added a proper variable declaration (instead of var, use const/let standard) and changed to calling property getvals.count because I would only use the form getvals[string] if the property name is actually a number or dash.

const 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"}';

const getvals = JSON.parse(res);

const count = parseInt(getvals.count);

const arr = [];

for(let i = 0;i < count;i++) {
  const line = {
    odds: getvals[`oddsa[${i}]`] + "/" + getvals[`oddsb[${i}]`],
    terms: getvals[`placeodds[${i}]`],
    position: getvals[`position[${i}]`]
  }
  arr.push(line)
}

console.log(arr)
TheTanadu
  • 554
  • 1
  • 7
  • 33