0

I am unable to find a reasonable solution as I am pulling JSON data from firebase and pulling it from node.js into an html file. I want to sort my data via a property called "value" by not sure how to access a sub-sub-value to sort by in JQuery and am wondering if someone could help lead me in the right direction.

{
    key: "a",
    {
         key: "ab",
         {
               value: 2
         }
         key: "ac",
         {
               value: 0
         }
    }
},
    {
    key: "b",
    {
         key: "bb",
         {
               value: 1
         }
    }
},

Output:

[{ac}, {bb}, {ab}]
4
  • 6
    (1) provide the sample input in text format, not an image. (2) Provide the expected output for that sample data. (3) There seems to be no array in your sample data, and object properties are by nature not ordered, so there seems nothing there to sort. Commented Jul 8, 2017 at 6:43
  • @trincot my apologies, I just realized arrays objects are distinct from what is being pulled from JSON. I now converted it into an array. I will update my question for (1) and (2) Commented Jul 8, 2017 at 6:52
  • Your input does not have valid syntax. It is some incompatible mix of object and array notation. What is the name of the property that has the nested object value? If you have trouble writing the correct notation, output JSON.stringify(myobject, null, 2) and paste that output into your question. Commented Jul 8, 2017 at 7:09
  • Also the desired output is invalid notation. Commented Jul 8, 2017 at 7:25

2 Answers 2

1

Both your input and desired output are expressed in an invalid notation (both in JSON and JavaScript syntax), so I'll have to make some assumptions.

You could use this recursive function, which will find all nested value properties, and collect those values together with the names of the parent properties in which they occur. Finally those pairs of data (parent key and value) are sorted:

function collectValues(obj, name) {
    return Object.entries(obj).reduce( (acc, [key, value]) => {
        return acc.concat(
            // recursively look into nested objects:
            Object(value) === value ? collectValues(value, key)
                // else, when key is 'value', collect that object
                : key == 'value' ? [[name, value]] 
                // otherwise ignore this value
                : []
        )
    }, []);
}

// Sample input
var input = [{
    "a": {
         "ab": {
            value: 2
         },
         "ac": {
            value: 0
         }
    }
}, {
    "b": {
        "bb": {
            value: 1
        }
    }
}];

var result = collectValues(input).sort( (a,b) => a[1] - b[1] );

console.log(result);

Sign up to request clarification or add additional context in comments.

1 Comment

Wow thank you for such an amazing solution, I am sorry if I was not able to convey my thought correctly as I am unfamiliar with the etiquette of JavaScript, but your code gave the headstart i needed to understanding the various sorting issues I had.
0

Mocking up your JSON from the original image:

 var data = {
        key1: {
            "a": { "deliveryshort": "12152017" },
            "b": { "deliveryshort": "10122015" },
            "c": { "deliveryshort": "11302016" },
            "d": { "deliveryshort": "09022014" }
        },
        key2: {
            "a": { "deliveryshort": "10102017" },
            "b": { "deliveryshort": "09102017" }
        },
    };
    function parseDate(dateStr) {
        var month = "" + dateStr[0] + dateStr[1];
        var day = "" + dateStr[2] + dateStr[3];
        var year = "" + dateStr[4] + dateStr[5] + dateStr[6] + dateStr[7];
        var result = new Date(year, month, day);
        return result;
    }
    function sortBy(data, property, converter) {
        var j = new Array();
        for (var item in data) {
            j.push([item, data[item], converter(data[item][property])]);
        }
        j.sort(function (a, b) { return a[2] - b[2] });
        return j;

    }
    function sortData(data) {
        var d = {};
        for (var item in data) {
            var sorted = sortBy(data[item], "deliveryshort", function (a) { return parseDate(a); });
           /*var normalized = new Array();
            for (var i = 0; i < sorted.length; i++) {
                var ni = sorted[i];
                var key = ni[0];
                var obj = ni[1];
                normalized[key] = obj;
            }*/
            d[item] = sorted;
        }

        console.log(d);
        return d;
    }
    sortData(data);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.