0

JSON callback returns me formatted dates and only it. Basing on given dates I have to build a tree structure, which will seperate different years, months and days. Oonly dates which are obtained in the callback are included in the tree.

I've write something like below on ServiceSucceedCallBack:

                        var daty = '';
                        var roczniki = '';
                        var miesiace = '';
                        var dni = '';
                        for (var i in result.Content) {
                            roczniki += '<ol id="lata">' + result.Content[i].getFullYear() + '</ol>';
                            miesiace += '<ol id="miesiace"><li>' + (result.Content[i].getMonth() + 1) + '</li></ol>';
                            dni += '<ol id="dni"><li>' + result.Content[i].getDate() + '</li></ol>';
                        }
                        var $st = $('#toolLeft');
                        $st.append(roczniki);
                        $('#lata').append(miesiace);
                        $('#miesiace').append(dni);

It gaves a tree view, but every date is written to the first node (first found year) and second problem is that I have no idea how to ommit duplication of datas. I mean, if some year has a place in the tree, than another date with the same year should go to the same node level, no create new one....

argh
  • 143
  • 4
  • 16

1 Answers1

0

It gaves a tree view, but every date is written to the first node

Use JSON.stringify to build a DOM, as in this question:

Javascript: Using reviver function, I seem can't get to alter all the keys, while concating the numbers

If some year has a place in the tree, than another date with the same year should go to the same node level, no create new one

Use a loop to insert each date as the key of an object literal, such as foo, then use JSON.parse to remove duplicate keys. Here is an example:

 var foo = {"2000-01-01":"good", "2001-09-11":"bad", "2000-11-02":"ugly", "2000-01-01":"jetson"}
 var bar = JSON.parse(JSON.stringify(foo) )
 var baz = JSON.stringify(bar)
Community
  • 1
  • 1
Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265