如何在javascript中循环检查数组?

时间:2018-02-07 02:28:42

标签: javascript arrays

我有两个数组:

var listOfObjects = [{ "name": "A", "data": [{'value1':'1','value2':'2'}] },
                     { "name": "B", "data": [{'value1':'','value2':''}] }];
var markedImage = ["A","B","C"];

这是我的代码:

function checkData() {
        var invalidDoc = [];

        for (var i = 0; i < listOfObjects.length; i++) {
            for (var j = 0; j < markedImage.length; j++) {
                if (listOfObjects[i].name== markedImage[i]) {
                    for (var k = 0; k < listOfObjects[i].data.length; k++) {
                        if (listOfObjects[i].data[k] == "") {
                            var invalidfile = markedImage.indexOf(listOfObjects[i].name);
                            invalidDoc.push(invalidfile);
                            break;
                        } else {
                            var valid = markedImage.indexOf(listOfObjects[i].name);
                            validDoc.push(valid);
                        }

                    }

                } else {
                    var invalidfile = markedImage.indexOf(listOfObjects[i].name);
                    invalidDoc.push(invalidfile);
                }
            }
        }
    }

我想要做的是检查两个阵列。

首先检查:如果markedImage中不存在listOfObjects中的项目,请将markedImage的索引推送到invalidDoc

第二项检查是:listOfObjects的数据不能为空,如果为空,则将markedImage的索引推送到invalidDoc

例如,

invalidDoc的预期结果是[1,2]。因为&#34; B&#34;的数据在listOfObjects中是空的,&#34; C&#34;在listOfObjects中不存在。 1和2是&#34; B&#34;的索引。和&#34; C&#34;在markedImage

1 个答案:

答案 0 :(得分:1)

检查此示例。我在markedImage数组上使用了reduce函数来查找listOfObjects中不存在的元素并检查data数组。

isDataEmpty是一个对每个data数组进行验证的函数。您可以根据所需的结果更改其逻辑。

var listOfObjects = [{ "name": "A", "data": [{'value1':'1','value2':'2'}]}, //not empty
                     { "name": "B", "data": [{'value1':'','value2':''}]},   //empty
                     { "name": "D", "data": []},                            //empty
                     { "name": "E", "data": [{'value1':'1','value2':'2'}]}, //not empty
                     { "name": "G"},                                        //empty
                     { "name": "H", "data": [{'value1':'','value2':''}, 
                                             {'value1':'1','value2':'2'}]}];//empty
var markedImage = ["A","B","C", "D", "E", "F", "G", "H"];

function isDataEmpty(data) {
    // You can add any validation conditions here.
    // I assume that your data is an array and that by empty
    // data you mean that array may be empty, undefined,
    // or have some objects with empty 'value1' or 'value2'
    return !data ||
           data.length === 0 || 
           data.some(function(d){return d.value1 === "" || d.value2 === ""});
};

function find(array, name) {
    for (var i in array) {
        if (array[i].name === name) return array[i];
    }
    return null;
}

var invalidDoc = markedImage.reduce(function(acc, cur, i) {
    var found = find(listOfObjects, cur);
    if (!found || isDataEmpty(found.data)) {
        acc.push(i);
    }
    return acc;
},[]);

console.log(invalidDoc);