javascript - Fastest way to add to and get values from a list of objects -


i'm getting started javascript objects. i'm trying store catalog inventory id , locations. think single object this:

var item = {     id: number,     locations: ["location1", "location2"]  }; 

i've started read bit still trying wrap head around it. not sure fastest way add new items list location, add new location existing item, while checking dupes. performance of getting locations later isn't critical. part of process running thousands of checks items id , location, performance key.

final question, i'm not sure if it's possible store in local storage. similar question, i'm not sure.

using lodash, should work determine if item id exists , append either new item array, or add new location:

var item = [{         id: 1,         locations: ["location1", "location2"]      },{         id: 2,          locations: ["location2", "location4"]      }];      function finditem(id){         return _.findindex(item, function(chr) {             return chr.id == id;         });     }     function additem(id,locations) {         var position = finditem(id);         if (position<0) {             item.push({                 id: id,                 locations: locations             })         } else {              item[position].locations = _.uniq(item[position].locations.concat(locations));         }     }      additem(2,['location292']);     additem(3,['location23']);     console.log(item); 

what search array of objects (item) id 1 passing additem() function, if found add new locations array existing item, if not it's creating new object new id , location.


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -