You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何过滤DynamoDB Scan结果数组,仅保留有效返回值

Solution

The core issue is that your Promise.all is collecting all resolved values—including the invalid 'x' or undefined entries. To get only the valid ['a','b'] arrays, you just need to filter the final result set and fix a common closure bug in your loop.

Step-by-Step Fixes:

  1. Fix the loop variable: Use let instead of var for index in your for loop. This ensures each iteration has its own copy of the index (avoids a bug where all scan operations end up using the final index value).
  2. Filter invalid results: After Promise.all resolves, filter out any values that aren't valid arrays matching your desired format.

Modified Code:

var results = [];
var array = ["a", "b"];

// Use let for index to fix closure issue
for (let index = 1; index <= 5; ++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) {
                    var uw = [];
                    console.log("Entered");
                    for (var i = 0; i < 2; i++) {
                        if (array.indexOf(data.Items[i].category) >= 0) {
                            console.log("pushing");
                            uw.push(data.Items[i].category);
                        }
                    }
                    // Return valid array only if it has both elements
                    return uw.length === 2 ? uw : null;
                } else {
                    // Return null for invalid cases
                    return null;
                }
            })
    );
}

Promise.all(results)
    .then((data) => {
        // Filter out null/undefined and keep only valid arrays
        const validResults = data.filter(result => Array.isArray(result) && result.length === 2);
        console.log("----------------------");
        console.log(validResults); // Output: [ ['a','b'], ['a','b'] ]
    })
    .catch(err => {
        console.log(err);
    });

Explanation:

  • Loop Variable Fix: Using let in the for loop creates a new binding for index in each iteration, so each scan operation uses the correct value of index when it runs.
  • Filtering: The filter method checks if each result is an array with exactly 2 elements (matching your valid ['a','b'] format). This removes any null, 'x', or undefined entries from the final output.

If you prefer to keep returning 'x' instead of null, adjust the filter to:

const validResults = data.filter(result => result !== 'x' && Array.isArray(result));

内容的提问来源于stack exchange,提问作者user3872094

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:50:09