3

I have a json file like:

"files": {
    "audio": {
        "number1": {
            "enabled": false,
            "priority": 5,
        },
        "number2": {
            "enabled": false,
            "priority": 1,
        },
        "number3": {
            "enabled": false,
            "priority": 2,
        }
    }
}

I convert this json file to xml with some function and also this function:

function extractFile(data){

    // adding for loop to check priority number?

    var datas = [];
    var element = data.files.audio;

    datas.push({
        name : 'Num1',
        enabled : element. number1.enabled? "true":"false"
    });
    datas.push({
        name : 'Num2',
        enabled : element. number2.enabled? "true":"false"
    });
    datas.push({
        name : 'Num3',
        enabled : element. number3.enabled? "true":"false"
    });

    return datas;
}

The output is:

   <file name="Num1" enabled="false"/>
   <file name="Num2" enabled="false"/>
   <file name="Num3" enabled="false”/>

How can I order them based on the priority number in the json?

The output needs to be like:

   <file name="Num2" enabled="false"/>
   <file name="Num3" enabled="false"/>
   <file name="Num1" enabled="false”/>
Dave Anderson
  • 11,836
  • 3
  • 58
  • 79
Geremy
  • 89
  • 7
  • 3
    Sort them before adding them to the `datas` array; you have what you need in `data` – x80486 Aug 19 '15 at 00:09
  • 1
    @ɐuıɥɔɐɯ Thanks for the comment would you please answer it in the Answer field section with this: function extractFile(data){ – Geremy Aug 19 '15 at 06:04

1 Answers1

2

You need a loop in your code that can iterate over data.files.audio in an order. It seems like this is an object rather than an array so try Iterate through object properties to create an array.

You will then need to sort the data.files.audio array using the answer to Sort array of objects by string property value in JavaScript.

EDIT

function extractFiles(data) {
    var elements = [];
    var files = [];

    // create an array of your source data objects
    for (var property in data.files.audio) {
        if (data.files.audio.hasOwnProperty(property)) {
            var p = data.files.audio[property];
            p.name = property.toString();
            elements.push(p);
        }
    }

    // create function to sort object array by priority
    function compareFiles(a, b) {
        if (a.priority < b.priority) return -1;
        if (a.priority > b.priority) return 1;
        return 0;
    }

    // create data for xml from sorted object array
    for (var e in elements.sort(compareFiles)) {
        files.push({
            name: elements[e].name,
            enabled: elements[e].enabled ? "true" : "false"
        });
    }

    return files;
}
Community
  • 1
  • 1
Dave Anderson
  • 11,836
  • 3
  • 58
  • 79
  • 1
    @DaveAndersonThanks for the answer I saw different post on stackoverflow, would you please edit your answer with the exact code in function extractFile(data){ , I couldn't fix it and still have problem! – Geremy Aug 19 '15 at 06:05
  • @Geremy so what problem did you have? I've included code for you but you should be trying to write this yourself if you want to learn. – Dave Anderson Aug 19 '15 at 21:47
  • @ DaveAnderson Thanks fixed it before and forgot to mark you answer , I had some misunderstanding by the way Thanks very much – Geremy Aug 19 '15 at 23:08