How to index multiple elements in an array or object using JavaScript / jQuery?

Background

I have an array of data in a result object returned by an Ajax call. The data is as follows:

{ Name="User1 Name1", FirstName="User1", Id="005400000001234567", more...} { Name="User2 Name1", FirstName="User2", Id="005400000001234568", more...} 

Where each element is as follows:

 { Id:"005400000001234567", Name:"User Name", FirstName:"User", LastName:"Name", Title:"Manager" } 

Question

I want to get data either using Id (return of one user) or Header (return of an array of users). What would be the best way to do this using JavaScript or jQuery?

Example

Here is what I tried to do so far:

 function GetAllUsers() { AllUsersById = new Object(); MyClass.MyAjaxMethod(function(result,event) { if(result) { j$(result).each(function(index,item) { AllUsersById[item.Id] = item; }); } }); } 

The code I have above is great for indexing by id, but I'm not sure what to do for Title.

Additional Information

In addition, by the way, there are about 1000 entries , and I need it to be effective enough. (This is one of the reasons I get data right away when the document is ready. However, I am not an expert on JavaScript or jQuery performance. Let me know if you have a better way.)

Any ideas? Thanks in advance!

+6
source share
4 answers

It looks like you are looking . grep () . Using .grep, you can create a generic function that will filter:

 function findInJson (json, key, value) { return $.grep(json, function (obj) { return obj[key] == value; }); } // With your data getting a specific user by id findInJson(yourJSON, "Id", "005400000001234567"); // Getting a set of users by title findInJson(yourJSON, "Title", "Manager"); 
+5
source

Create a vis-ร -vis class constructor function that encapsulates this data, and you can ask it to find users by name or id. To perform a quick search, you can create two lookup tables โ€” one for id and one for the title. Assuming a decent hash implementation, a search can be done in O(1) on average. The initial calculation is O (n), but the search is faster. He also uses a little more space because we are creating two additional maps. For 1000 objects this is not a problem. Again, if you do a lot more searches, this approach will be much faster.

Here's a simple implementation.

 function Users(users) { this.idMap = {}; this.titleMap = {}; this.users = users; var me = this; users.forEach(function(user) { this.idMap[user.Id] = this.idMap[user.Id] || []; this.idMap[user.Id].push(user); this.titleMap[user.Title] = this.titleMap[user.Title] || []; this.titleMap[user.Title].push(user); }.bind(this)); } Users.prototype.findByTitle = function(title) { return this.titleMap[title]; }; Users.prototype.findById = function(id) { return this.idMap[id]; }; 

To use it, create a Users object, passing it an AJAX response, and then query it using the findById and findByTitle .

 var users = new Users(responseData); users.findById("1"); users.findByTitle("SomeTitle"); 

Check out the worker.

+2
source

I think this should work. I really can not verify it without your data.

 function GetAllUsers() { AllUsersById = new Object(); AllUsersByType = new Object(); MyClass.MyAjaxMethod(function(result,event) { if(result) { j$(result).each(function(index,item) { // By Id AllUsersById[item.Id] = item; // By Type if (!AllUsersByTitle[item.Title]) { AllUsersByTitle[item.Title] = new Array(); } AllUsersByType[item.type].push() = item; }); } }); } 
0
source

If you have control over the returned data, it is better if you create it in the following format:

 var allUsers=[ {"005400000001234567":{ Name:"User Name", FirstName:"User", LastName:"Name",` Title:"Manager" }} ,{"005400000001234568":{ Name:"User2 Name2", FirstName:"User2", LastName:"Name2", Title:"Manager2" }} /*..etc.. */ ]; 

This way you avoid the loop ( $(result).each() ) inside GetAllUsers (which builds the array above). A title search can be performed efficiently by building a second form array:

 var byTitle=["title1":[0,1], "title1":[0,1], /*etc*/]; 

As you can see, each heading has a list of allUsers indexes. Then you just do:

 var allManagers = []; for(var i in byTitle["Manager"]) allManagers.push(allUsers[i]); 
0
source

Source: https://habr.com/ru/post/908462/


All Articles