简体   繁体   中英

Copying array of objects into another array is it reference type in JQuery

I have a array of objects TransactionVModel.FiltersList[] .
When I copy this array to another array fltrList[] and if I modify any of the object in array fltrList will it get reflect in Array TransactionVModel.FiltersList in JQuery ? For better clarity below is my example. As per me since it is a reference type it should update array TransactionVModel.FiltersList as well but in my scenario it is not happening, can I know why it is not happening ?

TransactionVModel.FiltersList is declared as ko.observableArray(); in my code.

function UpdateSelectedFilters(data) {
    var fltrList = [];
    fltrList = TransactionVModel.FiltersList();
    for (var i = 0; i < data.length ; i++) {
      var index = fltrList.indexOf(data[i]);
      if (index != -1) {
        var fltrObj = fltrList[index];
        var fltrValArr = [];
        fltrValArr = data.valueItems;
        for (var j = 0; j < fltrValArr.length; j++) {
        if (fltrValArr[j].IsSelected == true) {
        if (fltrObj.indexOf(fltrValArr[j]) != -1) {
            var selectedVal = fltrObj[fltrObj.indexOf(fltrValArr[j])];
            selectedVal.IsSelected = true;
                }
            } 
        }
    }
}

In my scenario I am updating selectedVal.IsSelected property but it is not reflecting the observableArray TransactionVModel.FiltersList .

You need to tell knockout that your array has changed with valueHasMutated :

function UpdateSelectedFilters(data) {
    var fltrList = [];
    fltrList = TransactionVModel.FiltersList();
    //...
    TransactionVModel.FiltersList.valueHasMutated();
}

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