开发者

To append an Key:Value pair to my array element only once!

var people =[{title:'Alan', hasChild:true},
            {title:'Alice', hasDetail:true},
            {title:'Amos',},
            {title:'Alonzo'},
            {title:'Brad'},
            {title:'Brent'},    
            {title:'Billy'},    
            {title:'Brenda'},   
            {title:'Callie'},
            {title:'Cassie'},   
            {title:'Chris'}];

By default my array does not contain any key,value pair which is header:'A' , header:'B' , `header'C' so on..开发者_JS百科. I add them at run time.

for (i = 0, len = people.length; i < len; i++) {
            var headerValue = people[i].header = people[i].title.charAt(0);
}

By doing this my array has a new key,value pair 'Header:'FirstLetter of the title'.

var people =[{title:'Alan', hasChild:true, header:'A'},
                {title:'Alice', hasDetail:true,header:'A'},
                {title:'Amos',header:'A'},
                {title:'Alonzo',header:'A'},
                {title:'Brad',header:'B'},
                {title:'Brent',header:'B'}, 
                {title:'Billy',header:'B'}, 
                {title:'Brenda',header:'B'},    
                {title:'Callie',header:'C'},
                {title:'Cassie',header:'C'},    
                {title:'Chris',header:'C'}];

I don't want to add the header:value to all the elementes, If i add the header:value to the element one whose title starts with 'A', then the rest of the elements whose title start's with A should be ignored. I want the array in this format.

var people =[{title:'Alan', hasChild:true, header:'A'},
                    {title:'Alice', hasDetail:true,},
                    {title:'Amos'},
                    {title:'Alonzo'},
                    {title:'Brad',header:'B'},
                    {title:'Brent'},    
                    {title:'Billy'},    
                    {title:'Brenda'},   
                    {title:'Callie',header:'C'},
                    {title:'Cassie'},   
                    {title:'Chris'}];

My updated code....

    function SortByName(x,y) {        
      return ((x.LastName == y.LastName) ? 0 : ((x.LastName > y.LastName) ? 1 : -1 ));    
    }

    function RenderPatientSearchData(PatientSearchResponse){
        var PatientSearchData = JSON.parse(PatientSearchResponse);
        var results = PatientSearchData['PatientSearchResult'];
        results.Patient.sort(SortByName);
        rowData = [];

        var prevHeader = '';
        for (i = 0, len = results.Patient.length; i < len; i++) {
            var headerValue = results.Patient[i].header = results.Patient[i].LastName.charAt(0);    
                if (headerValue !== prevHeader) {      
                    headerValue = results.Patient[i].header = results.Patient[i].LastName.charAt(0);    
             }
            prevHeader = headerValue;

....... some code... 

}


Keep track of the header value within the loop:

var prevHeader = '';
for (i = 0, len = people.length; i < len; i++) {
    var headerValue = people[i].title.charAt(0);
    if (headerValue !== prevHeader) {
      people[i].header = headerValue;
    }
    prevHeader = headerValue;
}

Note: to make this work, your people array has to be sorted beforehand

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜