2017-10-19 5 views
0

J'ai iciRéorganiser tableau par donné une autre propriété de tableau comme ordre javascript

1er tableau de 2 tableau est les principales données et 2ème tableau par la propriété doit être les bases de 1er ordre de données de tableau.

2ème échantillon de tableau:

var order_basis = [ 
{ tag:'vip' }, { tag:'home' } { tag:'work' } 
] 

1er données du tableau

var main_data = [ 
{ tag:'work',name:'sample',contact:'0987654',email:'[email protected]' }, 
{ tag:'home',name:'sample',contact:'0987654',email:'[email protected]' }, 
{ tag:'home',name:'sample',contact:'0987654',email:'[email protected]' }, 
{ tag:'work',name:'sample',contact:'0987654',email:'[email protected]' }, 
{ tag:'vip',name:'sample',contact:'0987654',email:'[email protected]' }, 
] 

résultat attendu est la base

de l'ordre 2ème balise tableau, il doit être ..

ReOrder(main_data ,order_basis){ 

//main code 

    return 
} 

résultat est

tag:'vip' name:'sample' contact:'0987654' email:'[email protected]' 
tag:'home' name:'sample' contact:'0987654' email:'[email protected]' 
tag:'home' name:'sample' contact:'0987654' email:'[email protected]' 
tag:'work' name:'sample' contact:'0987654' email:'[email protected]' 
tag:'work' name:'sample' contact:'0987654' email:'[email protected]' 

Merci de votre aide! ..

Répondre

0

Vous pouvez trier la principale fonction basée sur le tableau order_basis.

function getFormatted(main, order_basis){ 
 
    order_basis = order_basis.map(x => x.tag); 
 
    return main.sort(function(a, b){ 
 
    if(order_basis.indexOf(a.tag) > order_basis.indexOf(b.tag)) 
 
     return 1; 
 
    return -1; 
 
    }); 
 
} 
 

 
var order_basis = [{ tag: 'vip' }, { tag: 'home' }, { tag: 'work' }], 
 
main_data = [{ tag: 'work', name: 'sample', contact: '0987654', email: '[email protected]' }, { tag: 'home', name: 'sample', contact: '0987654', email: '[email protected]' }, { tag: 'home', name: 'sample', contact: '0987654', email: '[email protected]' }, { tag: 'work', name: 'sample', contact: '0987654', email: '[email protected]' }, { tag: 'vip', name: 'sample', contact: '0987654', email: '[email protected]' }]; 
 

 

 
console.log(getFormatted(main_data, order_basis));
.as-console-wrapper { max-height: 100% !important; top: 0; }

1

Vous pourriez prendre l'index des étiquettes de order_basis assortorder en utilisant un objet avec ces données.

var order_basis = [{ tag: 'vip' }, { tag: 'home' }, { tag: 'work' }], 
 
    main_data = [{ tag: 'work', name: 'sample', contact: '0987654', email: '[email protected]' }, { tag: 'home', name: 'sample', contact: '0987654', email: '[email protected]om' }, { tag: 'home', name: 'sample', contact: '0987654', email: '[email protected]' }, { tag: 'work', name: 'sample', contact: '0987654', email: '[email protected]' }, { tag: 'vip', name: 'sample', contact: '0987654', email: '[email protected]' }], 
 
    order = {}; 
 

 
order_basis.forEach(function (o, i) { order[o.tag] = i + 1 }); 
 

 
main_data.sort(function (a, b) { 
 
    return order[a.tag] - order[b.tag]; 
 
}); 
 

 
console.log(main_data);
.as-console-wrapper { max-height: 100% !important; top: 0; }