简体   繁体   中英

Finding and updating performance on deeply nested Arrays in Objects with findIndex

I have the following data structure:

const myData = [
        {
            "trips": [
                {
                    "destination": "Hungary",
                    "id": "34547",
                    "stars": 0
                },
                {
                    "destination": "Hungary",
                    "id": "14542",
                    "stars": 0
                },
                {
                    "destination": "Hungary",
                    "id": "88247",
                    "stars": 0
                },
                {
                    "destination": "Hungary",
                    "id": "11447",
                    "stars": 0
                },
            ],
            "descr": "Holidays",
            "id": "243567"
        },
    ]

Assuming we have N objects with unique IDs:

Given the item id , the trip id , and a replacement trip object, find and replace the trip object.

Example:

const itemId = 243567;
const tripId = 14542;

const replacement = { 
  destination: Beijing
  id: 14542
  stars: 4
};;

My solution was the following:

 const myData = [{ "trips": [{ "destination": "Hungary", "id": "34547", "stars": 0 }, { "destination": "Hungary", "id": "14542", "stars": 0 }, { "destination": "Hungary", "id": "88247", "stars": 0 }, { "destination": "Hungary", "id": "11447", "stars": 0 }, ], "descr": "Holidays", "id": "243567" }]; const itemId = 243567; const tripId = 14542; const replacement = { destination: "Beijing" id: 14542 stars: 4 }; const itemIndex = myData .findIndex(element => element.id === itemId); const tripIndex = myData[itemIndex].trips .findIndex(element => element.id === tripId); Object.assign(myData[itemIndex].trips[tripIndex], replacement); 

How would this solution perform and are there faster ways to implement it?

If you only need to perform one such look-up and mutation for a given data set, then what you currently do is fine.

If however, you will perform multiple look-ups and mutations within the same data set (so before it gets reloaded), then you should key the data by id and trip id. For that you could use this function, which you should call once after the data set is loaded:

 function hashData(myData) { const result = {}; for (const row of myData) { const obj = result[row.id] = {}; for (const trip of row.trips) { obj[trip.id] = trip; } } return result; } // Sample data const myData = [{ "trips": [{"destination": "Hungary", "id": "34547", "stars": 0 },{"destination": "Hungary", "id": "14542", "stars": 0 },{"destination": "Hungary", "id": "88247", "stars": 0 },{"destination": "Hungary", "id": "11447", "stars": 0}], "descr": "Holidays", "id": "243567"}]; // Key it by id and trip id: const hash = hashData(myData); // Mutate one particular entry: Object.assign(hash[243567][88247], { destination: 'PARADISE', id: "9999", stars: 5 }); // Display result console.log(myData); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

If you don't mind more verbose code, then replacing Object.assign with individual assignments will give better performance in current browsers:

const obj = hash[243567][88247];
obj.destination = 'PARADISE';
obj.id = "9999";
obj.stars = 5;

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