I'm trying to create a JSON from the data returned by DynamoDB. and I'm confused on how to create it.
Currently, I get data into an array and populate the JSON below is my code.
var res = {};
dynamodb.scan(params).promise().then(function (data) {
var ingred = data.Items[0].ingredients;
res = {
[ingred]: {}
};
ingred.max = Math.max(...data.Items.map(({max}) => parseFloat(max))) + "";
ingred.min = Math.min(...data.Items.map(({min}) => parseFloat(min))) + "";
console.log(JSON.stringify(res, null, 4));
})
Here when I run this, I get the output as below.
{
"a": {}
}
{
"b": {}
}
{
"c": {}
}
and when I change my code to the below
var res = {};
dynamodb.scan(params).promise().then(function (data) {
var ingred = data.Items[0].ingredients;
console.log(ingred);
ingred = {};
ingred.max = Math.max(...data.Items.map(({ max }) => parseFloat(max))) + "";
ingred.min = Math.min(...data.Items.map(({ min }) => parseFloat(min))) + "";
console.log(JSON.stringify(ingred, null, 4));
})
the output I get is as below
a
{
"max": "14",
"min": "8.5"
}
b
{
"max": "1.98",
"min": "0.37"
}
c
{
"max": "155",
"min": "6"
}
I am trying to achieve the result as below.
{
"a": {
"max": "14",
"min": "8.5"
},
"b": {
"max": "1.98",
"min": "0.37"
},
"c": {
"max": "155",
"min": "6"
}
}
please let me know on how I can achieve this.
Thanks
Sandip Ghosh
7297 silver badges13 bronze badges
asked Jan 10, 2018 at 6:08
user3872094
3,3739 gold badges39 silver badges80 bronze badges
4 Answers 4
You just need to set the ingred in the res
var res = {};
dynamodb.scan(params).promise().then(function (data) {
var ingred = data.Items[0].ingredients;
res[ ingred ] = {};
res[ ingred ].max = Math.max(...data.Items.map(({ max }) => parseFloat(max))) + "";
res[ ingred ].min = Math.min(...data.Items.map(({ min }) => parseFloat(min))) + "";
console.log(JSON.stringify(res, null, 4));
})
answered Jan 10, 2018 at 6:16
gurvinder372
68.6k11 gold badges78 silver badges98 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Try to do this:
var res = {};
dynamodb.scan(params).promise().then(function (data) {
var ingred = data.Items[0].ingredients;
res[ingred] = {};
res[ingred].max = Math.max(...data.Items.map(({ max }) => parseFloat(max))) + "";
res[ingred].min = Math.min(...data.Items.map(({ min }) => parseFloat(min))) + "";
console.log(JSON.stringify(res, null, 4));
})
answered Jan 10, 2018 at 6:13
Sergii Rudenko
2,6841 gold badge24 silver badges25 bronze badges
Comments
Add max and min to your object in res[ingred].
var res = {};
dynamodb.scan(params).promise().then(function (data) {
var ingred = data.Items[0].ingredients;
res[ingred] = {};
res[ingred].max = Math.max(...data.Items.map(({ max }) => parseFloat(max))) + "";
res[ingred].min = Math.min(...data.Items.map(({ min }) => parseFloat(min))) + "";
console.log(JSON.stringify(res, null, 4));
})
answered Jan 10, 2018 at 6:15
Hassan Imam
22.6k6 gold badges45 silver badges53 bronze badges
Comments
Just store a and b into an object and store that into a global object:
const result = {};
dynamodb.scan(params).promise().then(function (data){
const ingred = data.Items[0].ingredients;
result[ingred] = {
max: Math.max(...data.Items.map(({ max }) => parseFloat(max))),
min: Math.min(...data.Items.map(({ min }) => parseFloat(min))),
};
})
answered Jan 10, 2018 at 6:23
Jonas Wilms
139k20 gold badges164 silver badges164 bronze badges
Comments
lang-js
data.Items[0]and another sample for the expected output will help people answer this question better.