0

我有一个具有重复键值对的对象。我需要遍历它并仅针对一个条件评估索引 [0] :

var cells = {"cells": [{"key": "-18", "key1": "-9", "key2": "-51", "key3": "23"}, {"key": 
"-18", "key1": "-9", "key2": "-51", "key3": "23"}, {"key": "-18", "key1": "-9", "key2": "-51", 
"key3": "23"}]};

到目前为止我的代码:

var result = true;

if (cells instanceof java.util.List && !cells.isEmpty()) {
 for (var i = 0; i < cells.size(); i++) {
    var cell = cells.get(i);

    var key = cell.get("key");
    var primeKey = key.get[0];
    if (primeKey) {
        if (parseInt(primeKey) < 5) {
            result = false;
            break;
        }
    } else {
        result = false;
        break;
    }
}
} else {
result = false;
}

return result;

我得到的错误是:评估期间遇到的错误:TypeError: Cannot read property "0.0" from undefined (#12)

4

1 回答 1

0

var primeKey = key.get[0];是荒谬的,因为"key"值对不是要迭代的数组。但是,如果我们简单地将i迭代器更改为 index 0like,那么 var cell = cells.get(0);我们可以仅针对 index 评估条件0。无需遍历整个嵌套循环,因为我们只与 index 进行比较0。详情见下文:

var cells = {"cells": [{"key": "-18", "key1": "-9", "key2": "-51", "key3": "23"}, {"key": 
"-18", "key1": "-9", "key2": "-51", "key3": "23"}, {"key": "-18", "key1": "-9", "key2": "-51", 
"key3": "23"}]};

var result = true;

if (cells instanceof java.util.List && !cells.isEmpty()) {
 for (var i = 0; i < cells.size(); i++) {
    var cell = cells.get(0);

    var key = cell.get("key");
    if (primeKey) {
        if (parseInt(primeKey) < 5) {
            result = false;
            break;
        }
    } else {
        result = false;
        break;
    }
}
} else {
result = false;
}

return result;
于 2021-09-17T01:08:26.557 回答