JS outputs the data in the array in a certain order.

the data you get from ajax goes like this:

[
    {"appType":"D201005" , "appName":"1131xxxx"},
    {"appType":"1030001" , "appName":"133XXXX9527"},
    {"appType":"0201008" , "appName":"195485233"},
    {"appType":"1030001" , "appName":"78321245"},
    {"appType":"1030001" , "appName":"25481166"},
    {"appType":"10300361" , "appName":"371203XXXX0057667X"},
    {"appType":"1030001" , "appName":"789132154"},
]

for example, I want to show my ID card number first on the page, followed by mobile phone number, WeChat account, Wechat ID.
wants to write an algorithm, because the number of data taken out each time is not fixed, sometimes one less, sometimes two less, but I want the order is: ID card number, mobile phone number, WeChat account, Wechat ID, order, how do you write it?
Thank you for your answer. You may have misunderstood me. The appType in front of me is the only logo. I got a lot of inspiration from you and came up with a more complicated way

Mar.24,2021

arr.sort ()

        const arr = [{
            'appType': 'D201005',
            'appName': ''
        }, {
            'appType': '1030001',
            'appName': ''
        }, {
            'appType': '0201008',
            'appName': 'ID'
        }, {
            'appType': '1030001',
            'appName': ''
        }, {
            'appType': '1030001',
            'appName': ''
        }, {
            'appType': '10300361',
            'appName': ''
        }, {
            'appType': '1030001',
            'appName': ''
        }]
        
        const order = {
            '': 0,
            '': 1,
            '': 2,
            'ID': 3,
        }
        arr.sort((a, b) => {
            return order[a.appName] - order[b.appName]
        })

?

var arr  = ['','','',];
var newData = [];
for(var i of arr.keys()){
    for(var j of data.keys()){
        if(arr[i] == data[j].appName){
            newData.push(data[j]);
            
        }
    }

}

the most straightforward idea, declare that four arrays are used to store four types of values, do a traversal, push the same values into the same array, and finally concatenate


with the contact function when returned.

result:
clipboard.png
Code:

var arr = [
    {'appType':'D201005' , 'appName':''},
    {'appType':'1030001' , 'appName':''},
    {'appType':'0201008' , 'appName':'ID'},
    {'appType':'1030001' , 'appName':''},
    {'appType':'1030001' , 'appName':''},
    {'appType':'10300361' , 'appName':''},
    {'appType':'1030001' , 'appName':''},
];
function sort(arr,sort){
    var map = {};
    for(var i in arr){
        var data = arr[i];
        var list = map[data.appName];
        if(!list){
            map[data.appName] = [ data ];
        }else{
            list.push(data);
        }
    }
    
    var result = [];
    for(var i in sort){
        var k = sort[i];
        if(k in map){
            result = result.concat( map[k] );
        }
        
    }
    return result;
}

console.log( sort(arr,",,,ID".split(",")) );

arr can increase or decrease data at will


for in performance is a little low, use object.keys

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b38976-2c10a.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b38976-2c10a.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?