Duplicate data when traversing JSON (JavaScript)

0

I have a JSON in which I keep a series of errors. I'm going to export them to a CSV, so I need the values of all the fields (they will be contained in the error and others that are not), for them I have conditioned it with nested IFs.

   var errorList = '[{"propertyName":"customerData.nifCif","error":"wrong","detail":"","value":"72007817D"},'{"propertyName":"address","error":"wrong","detail":"","value":"direcciomndhdjj"}]';
   var objErrList = JSON.parse(errorList);

for(var j = 0; j < objErrList.length; j++) {
    if (objErrList[j].propertyName === "customerData.nifCif") {

        if (objErrList[j].error === "wrong") {
                console.log("wrong nif");
        } else if (objErrList[j].error === "empty") {
                console.log("empty nif");
        }

    } else {
        console.log("dni correcto");
    }
  if (objErrList[j].propertyName === "productType") {

        if (objErrList[j].error === "wrong") {
            console.log("wrong productType");
        } else if (objErrList[j].error === "empty") {
            console.log("empty productType");
        }

    } else {
        console.log("productType correcto");
    }

      if (objErrList[j].propertyName === "address") {

        if (objErrList[j].error === "wrong") {
            console.log("wrong address");
        } else if (objErrList[j].error === "empty") {
            console.log("empty address");
        }

    } else {
        console.log("address correcto");
    }
}

The problem is that I get multiplied data (if you execute that code you can check it. Only I need you to take a line , in plan:

wrong nif, productType correcto, wrong address

Let's see that once you check in all the propertyName that exists "nif" for example, save its value and do not check it again. What is the solution so that the data checks are not repeated?

    
asked by Norak 20.09.2017 в 17:58
source

1 answer

1

As I see it, this should be like this

var errorList = '[{"propertyName":"customerData.nifCif","error":"wrong","detail":"","value":"72007817D"},'{"propertyName":"address","error":"wrong","detail":"","value":"direcciomndhdjj"}]';
   var objErrList = JSON.parse(errorList);

for(var j = 0; j < objErrList.length; j++) {
    if (objErrList[j].propertyName === "customerData.nifCif") {

        if (objErrList[j].error === "wrong") {
                console.log("wrong nif");
        } else if (objErrList[j].error === "empty") {
                console.log("empty nif");
        } else {
        	console.log("dni correcto");
    	}

    }
  if (objErrList[j].propertyName === "productType") {

        if (objErrList[j].error === "wrong") {
            console.log("wrong productType");
        } else if (objErrList[j].error === "empty") {
            console.log("empty productType");
        } else {
        	console.log("productType correcto");
    	}

    } 

      if (objErrList[j].propertyName === "address") {

        if (objErrList[j].error === "wrong") {
            console.log("wrong address");
        } else if (objErrList[j].error === "empty") {
            console.log("empty address");
        } else {
        	console.log("address correcto");
	}
    }

}
    
answered by 20.09.2017 в 18:05