I have JSON and JSON array and I want to push every elements of JSON to JSON array with some condition.
Here is my code:
let json = { Jack: 10, Merry: 29, Charles: 37, Lahm: 0 }
let jsonArray = [
{
match: {
Ashley: 46,
},
},
]
for (const key in json) {
if (json[key] !== 0) {
jsonArray.push({
match: `{${key}: ${json[key]}}`,
})
}
}
I get the following response:
[
{ match: { Ashley: 46 } },
{ match: '{Jack: 10}' },
{ match: '{Merry: 29}' },
{ match: '{Charles: 37}' }
]
The problem is that pushed elements has single quotes which I don't want. Is there a way to push those elements without quotes?