diff --git a/src/surveyHelper.ts b/src/surveyHelper.ts index d0f7bbc657..a5bc8093dc 100644 --- a/src/surveyHelper.ts +++ b/src/surveyHelper.ts @@ -170,39 +170,23 @@ export class SurveyHelper { dest.splice(0, dest.length); return; } - var destHash = SurveyHelper.applyItemValueArrayCreateHash(dest); - var srcHash = SurveyHelper.applyItemValueArrayCreateHash(src); - for (var i = 0; i < dest.length; i++) { - var ind = srcHash[dest[i].value]; - dest[i].srcIndex = ind === undefined ? -1 : ind; + if (dest.length > src.length) { + dest.splice(src.length, dest.length - src.length); } - var insertIndex = 0; - for (var i = 0; i < src.length; i++) { - var ind = destHash[src[i].value]; - if (ind === undefined) { - dest.splice(insertIndex, 0, src[i]); - insertIndex++; - } else { - insertIndex = ind + 1; + if (dest.length < src.length) { + var insertedArray = []; + for (var i = dest.length; i < src.length; i++) { + insertedArray.push(src[i]); } + dest.splice.apply(dest, [dest.length, 0].concat(insertedArray)); } - for (var i = dest.length - 1; i >= 0; i--) { - if (dest[i].srcIndex === -1) { - dest.splice(i, 1); - } else { - if (dest[i].srcIndex > -1 && src[dest[i].srcIndex].hasText) { - dest[i].text = src[dest[i].srcIndex].text; - } + for (var i = 0; i < dest.length; i++) { + if (dest[i].value != src[i].value) { + dest[i].value = src[i].value; } + dest[i].text = src[i].hasText ? src[i].text : ""; } } - private static applyItemValueArrayCreateHash(arr: Array) { - var res = {}; - for (var i = 0; i < arr.length; i++) { - res[arr[i].value] = i; - } - return res; - } public static disableSelectingObj(obj: Survey.Base) { obj["disableSelecting"] = true; } diff --git a/tests/propertyEditors/propertyEditorsTests.ts b/tests/propertyEditors/propertyEditorsTests.ts index 7ddc1177b0..594baad6e0 100644 --- a/tests/propertyEditors/propertyEditorsTests.ts +++ b/tests/propertyEditors/propertyEditorsTests.ts @@ -2491,4 +2491,13 @@ QUnit.test("SurveyHelper.applyItemValueArray", function (assert) { [1, { value: 2, text: "item 2" }, { value: 4, text: "item 4" }], 7 ); + testSetFunc([1, 2, 2, 3, 4, 5, 6], [1, 2, 2, 3], 8); + testSetFunc([1, 2, 3, 4, 2, 5, 6], [1, 2, 3, 2], 9); + console.log(q2.toJSON()); + testSetFunc( + [1, 2, 13, 14, 5, 6], + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], + 10 + ); + console.log(q2.toJSON()); });