I cant seem to logic this one out. I am calling a database and getting response 'resp' which is an array of key value pairs. Response is what I am returning from the function I have below. Most of the data I just wish to return as an array of objects containing two key value pairs (Type/data), and that is achieved elsewhere in the code. However, if the value of Type is count or counted, I wish to group these in its own array
if (resp[i].Type === "count" || resp[i].Type === "counted" && Response[i-1].Type != "grouping" ) {
ItemGroup.push({
Type: resp[i].Type,
data : resp[i].data})
var grouping = "grouping"
Response.push({ Type: grouping,
data : ItemGroup
}
}
what I want is shown below, however, my code is displaying this but replicating the whole object out 3 times... I am new to JS, so still figuring this all out
{
"Type": "grouping",
"data": [
{
"Type": "count",
"data": "this is the data"
},
{
"Type": "count",
"data": "more data"
},
{
"Type": "counting",
"data": "input values"
}
]
},