I am receiving a JSON to my nodeJS app in the following format from an ajax call; where i receive an array and i send it using
$.ajax({
type:'POST',
url:'/check',
data:JSON.stringify(array1),
contentType: "application/json; charset=utf-8",
dataType: "json"})
})
i receive it as follows :
[{ key: 'name', value: 'lorem ipsum' }
{ key: 'language', value: 'en' }
{ key: 'color', value: 'red' }
{ key: 'resolution', value: [ 1920, 1080 ] } ]
I want to save each of these values in variables, something like this :
app.post('/check', function(req, res)
{
var obj = req.body;
var keys = Object.keys(obj);
for (var i = 0; i < keys.length; i++) {
console.log(keys[i])
// what i want to do:
// if (keys[i] == 'name') {
// var name = value of this key,
// in this example
// var name = "lorem ipsum"
// var language = "en"
// var color = "red"
// var resolution = [ 1920, 1080 ]
}
res.send("ok");
});
I am not sure how to loop through the keys of the JSON and associate the value for the key in my code
Currently console.log(keys[i])
returns an index number, which is not useful to me