Citysearch historical issues (array of objects to weight)

First, the problem

Citysearch historical issues (history save localStorage)
- with an array of objects wrapped in urban (city contains id and name), record 5, does not contain duplicate city, and the placement of an array of the latest front
- idea: to determine whether there is a history that the array is empty, empty directly into
- is not empty, it is determined whether the object already exists an array of new urban array foremost, there is the array to move heavy and there is no directly into the array unshift
- Note point: to determine a target the existence of elements in the array
- array of objects to heavy

// EG: urban search history array 
var List = [ 
    {the above mentioned id: '0001', name: 'Beijing' }, 
    {the above mentioned id: '0002', name: 'Shanghai' }, 
    {the above mentioned id: '0003', name: ' Shenzhen ' }, 
    {ID: ' 0004 ', name:' Canton ' } 
]; 
// New city search history 
var cityObj = { 
    ID: ' 0003 ' , 
    name: "Shenzhen' 
} 

// method a: circular array, with id id of each element of the list of comparison cityObj, -> are not the same and the same 
for ( var I = 0; I <List.length; I ++ ) {
     IF (cityObj.id list == [I].    id) {    //Remove all the same 
        list.splice (I,. 1);     // delete the element 
        i-- ; 
    } 
} 
list.unshift (cityObj);     // the latest added cities searches 
the let historyStr = '' ;
 IF (List.length >. 5 ) { 
    historyStr = the JSON.stringify (list.slice (0,. 5 )); 
} the else { 
    historyStr = the JSON.stringify (List); 
} 
the console.log (List, historyStr);

 

Guess you like

Origin www.cnblogs.com/xinghong/p/11366837.html