Reputation: 3351
I'm having the below data in dynamo DB
id category value rule
1 a 5 1
2 b 5 1
3 c 5 1
4 a 5 2
5 b 5 2
6 a 2 3
7 b 2 3
8 a 3 4
9 b 3 4
10 c 2 4
11 d 2 4
12 b 5 5
13 c 5 5
Here my target is as below.
rule
with size 2
. once I get that rule, check if it has only a
and b
in category. Ignore the rest.Here is my current query.
var array=["a","b"];
for (index = 1; index <= 5; ++index) {
console.log(index);
var params1 = {
TableName: "MyCatTable",
FilterExpression: "#rule=:rule",
ExpressionAttributeNames: {
"#rule": "rule"
},
ExpressionAttributeValues: {
":rule": String(index)
}
};
results.push(dynamodb.scan(params1).promise().then(function (data) {
if (data.Items.length == 2 && (array.indexOf(data.Items[0].category) >= 0)) {
var uw = data.Items;
return uw;
}
}));
}
return Promise.all(results);
}).then((data) => {
var res;
console.log("----------------------");
console.log(data);
})
when I run this it is returning me the rules 2
, 3
and 5
. But I need only 2
and 3
. since 5
has b
and c
instead of a
and b
.
or is there any other way of filtering out the json response that I get?
please let me know where am I going wrong and how can I fix this.
Thanks
Upvotes: 1
Views: 88
Reputation: 2801
The issue is with this part of the code:
data.Items[0].category
it only checks the category of the first element on the "data.Items" array, you need to filter the array by checking the category of each element instead of just the first one. The reason that 5 is returned is because the first element of 5 is 'b' which exists in your array.
*edited - code fragment as requested (this should be a rough outline of how the code should look. there might be a few errors though)
if (data.Items.length == 2) {
var uw = [];
for(var x in data.Items){
if(array.indexOf(x.category) >= 0) uw.push(x);
}
return uw;
}
Upvotes: 1