0

I have two array:

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

and this is my code:

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);
                }
            }
        }
    }

What I want to do is check both array.

First check is : If item in markedImage is not exist in listOfObjects, push the index of markedImageinto invalidDoc.

Second check is : the data of listOfObjects cannot be empty, if it is empty push the index of markedImageinto invalidDoc.

For example,

Expected result for invalidDoc is [1,2]. Because data of "B" in listOfObjects is empty, "C" is not exist in listOfObjects. 1 and 2 is the index of "B" and "C" in markedImage.

9
  • a expected output array is better than words below: invalidDoc will have 1 and 2 inside. Because data of "B" in listOfObjects is empty, "C" is not exist in listOfObjects. 1 and 2 is the index of "B" and "C" in markedImage. Commented Feb 7, 2018 at 2:40
  • @xianshenglu he did. For example, invalidDoc will have 1 and 2 inside. Commented Feb 7, 2018 at 2:41
  • you mean [1,2]? Commented Feb 7, 2018 at 2:42
  • @xianshenglu yes.. Commented Feb 7, 2018 at 2:42
  • Is you data an array or a string? Commented Feb 7, 2018 at 2:50

1 Answer 1

1

Check this example. I used the reduce function over the markedImage array to find elements that are not present in the listOfObjects and check the data arrays.

isDataEmpty is a function which does the validation of every data array. You can change its logic according to the desired result.

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);

Sign up to request clarification or add additional context in comments.

12 Comments

for some reason.. i can't use the => arrow function.
Then you can replace it with a regular function, eg (data) => { becomes function(data) {
@Xion Well, then you can change them to var, all logic stays the same. Check my updated code.
The find methos is not supported in IE...and what if my listOfObjects is like this [{ "name": "A", "data": [{'value1':'1','value2':'2'},{'value1':'','value2':''}] }]; .
@Xion should this object be considered emty?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.