简体   繁体   中英

How can i solve this scenario without nested for loop in javascript

var selectedRows = gridOptions.api.getSelectedRows(); //this is an array

selectedRows.forEach(function(selectedRow, index) {
    if (dataSampleAfterUpdate.length == 0) {
        dataSampleAfterUpdate.push(selectedRow);
    }
    for (var x = 0; x < dataSampleAfterUpdate.length; x++) {
        if (dataSampleAfterUpdate[x].Id == selectedRow.Id) {
            dataSampleAfterUpdate[x] = selectedRow;
        } else {
            dataSampleAfterUpdate.push(selectedRow);
        }
    }
});

Actually this code works for 10 or 20 records. But in case of 500 records the page hangs. Is there any alternate way of using nested for loop?Please help

You have an O(n^2) complexity algorithm. Instead of array, you can keep track of your visited items in an object, so the lookup can be done in O(1) complexity and the need for inner for loop is eliminated.

To the best of my understanding of your code you likely need the following simplified code that runs in O(n) time:

var selectedRows = gridOptions.api.getSelectedRows(); //this is an array
var obj = Object.fromEntries(dataSampleAfterUpdate.map(e => [e.Id, e]));

selectedRows.forEach( function(selectedRow, index) {
    obj[selectedRow.Id] = selectedRow;
});

dataSampleAfterUpdate = Object.values(obj);

Here is an improvement to an earlier answer:

var selectedRows = gridOptions.api.getSelectedRows(); //this is an array

var arr = [...dataSampleAfterUpdate, ...selectedRows];
var obj = Object.fromEntries(arr.map(e => [e.Id, e]));

dataSampleAfterUpdate = Object.values(obj);

This first combines the two arrays into one single array arr . Then creates an object by grouping the array by their ids, to remove duplicates. By design it replaces the previous values by the one encountered next.

To understand this better you can expand and run the code snippet below. Individual steps are explained.

 var dataSampleAfterUpdate = [{Id: 1, Data: "a"}, {Id: 2, Data: "b"}, {Id: 3, Data: "c"}]; var selectedRows = [{Id: 2, Data: "bb"}, {Id: 4, Data: "dd"}]; // Combine the arrays into a nested array with eventual key value pairs (key = id). var arr = [...dataSampleAfterUpdate, ...selectedRows]; console.log("Log 1:", JSON.stringify(arr)); // Converts into a nested array with eventual key value pairs (key = id). var temp = arr.map(e => [e.Id, e]); console.log("Log 2:", JSON.stringify(temp)); // Converts key value pair to object. Removes duplicate ids. By design object can only hold one value per key. var obj = Object.fromEntries(temp); console.log("Log 3:", JSON.stringify(obj)); // Converts back to array of values. dataSampleAfterUpdate = Object.values(obj); console.log("Log 4:", JSON.stringify(dataSampleAfterUpdate));

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