简体   繁体   中英

Compare two json arrays in loop

I have the following two JSON array objects.

questions.QuestionAlternatives:

[{
  Id: "53e169ce-acb1-4cb3-b7b9-9314a84660e0",
  AlternativeText: "Opt2",
  RiskScore: "1"
}, {
  Id: "a1c30d1f-e585-4a74-a3a5-9f2d26fb804f",
  AlternativeText: "Opt3",
  RiskScore: "2"
}, {
  Id: "85385ec6-a4d0-489b-bf08-b4a7321e9cfe",
  AlternativeText: "Opt1",
  RiskScore: "1"
}]

optionText:

[{
  OptionText = "Opt2", Id = "53e169ce-acb1-4cb3-b7b9-9314a84660e0"
}, {
  OptionText = "Opt3", Id = "a1c30d1f-e585-4a74-a3a5-9f2d26fb804f"
}, {
  OptionText = "Opt1", Id = "85385ec6-a4d0-489b-bf08-b4a7321e9cfe"
}, {
  OptionText = "opt5", Id = ""
}]    

Now I want to compare this in the following code:

for (var i = 0; i < json[0].Questions.length; i++) {
    if (json[0].Questions[i].Id == id) {
        //Compare here
    }
}

I want to check If the Id in optionText exists in QuestionAlternatives. If It does exist, I want to set the OptionText with the value from the OptionText inside optionText. A simple update.

If It does not exists, I want to add the object with the empty Id to the QuestionAlternative.

Anyone who can push me toward right direction?

I have tried this comparsion:

                for(var opt = 0; opt < optionText.length; opt++) {
                    for(var a = 0; a < json[0].Questions[i].QuestionAlternatives.length; a++) {
                        if(optionText[opt].Id ==  json[0].Questions[i].QuestionAlternatives[a].Id) {
                            console.log("exists");
                        } else {
                            console.log("does not");
                        }
                    }
                }

But that gives me the following:

(3) exists
does not
(3) exists
does not
exists

Use JSON.NET. You can easily parse your json string to JArray and with for loop you can compare values.

I'm not sure I fully understand, but here's how I would recommend you go about it in pseudo code.

for each object in optionText
  id = object.id
  filteredAlternatives = QuestionAlternatives.filter(question.id === id)
  if(filteredAlternatives.length > 0) 'Id in optionText exists in QuestionAlternatives!'

Look into Array.prototype.filter()

You could use an object/hash-table to make this kind of operation more efficient (albeit making it a bit more memory intensive):

// var optionText = JSON.parse(...);
// questions.QuestionAlternatives = JSON.parse(...);

// questionAlternatives { Id => question alternative }
var questionAlternatives = {};
var questionAlternative;
for(var z = 0; z < questions.QuestionAlternatives.length; z++){
    questionAlternative = questions.QuestionAlternatives[z];
    questionAlternatives[questionAlternative.Id] = questionAlternative;
}

var option;
for(var z = 0; z < optionText.length; z++){
    option = optionText[z];
    if(questionAlternatives[option.Id]){
       // found alternative
       // something like this:
       questionAlternatives[option.Id].OptionText = option.OptionText;
    }else{
       // id not found on alternatives
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM