0

Related Retrieve two lists, sort and compare values, then display all the results

The question in the related post was how to combine two lists and sort them. The code referenced each item on each list. So, when I got the result, I could manipulate it.

The best solution used console.log(JSON.stringify(result,null,2)); to return the result, nicely combined and sorted.

Trouble for me is being able to translate that back into something I can work with. I can get the result into a variable and display it on the page, but it's the raw output : [ { "Title": "apple", "Type": "rome", "State": null }, ...

Have tried 'JSON.parse(result);' where result is the variable that is used to handle the combination and sorting of the two lists. All that gives is an invalid character error on the line. Also looked at the 'replace' option. That just confused me, tmi. Tried setting a variable directly on the result (so those who know are laughing) 'var foo = result;' That returns object, object.

The desired end result would be to end up with each item separate so I can put them in a table (or a list) on my html page with blanks in any column where there is no data.

I know there has to be a simple, easy way to do this without 200 lines of transformation code. But I can't find a clear example. Everything I'm seeing is for +experts or uses a super simple array that's typed into the code.

Is there a way to attach something like this (from my original) to the result instead of using JSON.stringify? What other step(s) am I missing in being able to extract the fields from JSON.stringify using JSON.parse?

}).success(function (data) {

    var title = '';
    var type = '';
     $.each(data.d.results, 
    function (key, value) {

    title += "Title: " + value.Title + "<br/>";
    type += "Type: " + value.Type  + "<br/>";
    }); 

$("#tdtitle").html(title);
$("#tdtype").html(type);
Community
  • 1
  • 1
Terri
  • 354
  • 6
  • 18
  • Can you describe the error when you try use JSON.parse? If the letter is u there's a good chance it's just the variable is undefined. – David Feb 03 '16 at 22:28
  • It's hard to figure out where you are struggling. Could you provide an example of the expected for `data`. What do you get in your `$.each()` loop? – Twisty Feb 03 '16 at 22:40
  • If you used `dataType: 'json'` in the AJAX call, `result` is already a Javascript array, you don't need to parse it. Just access the array elements. Your code should work as written. – Barmar Feb 03 '16 at 23:03
  • Have tried 'JSON.parse(result);' where result is the variable that is used to handle the combination and sorting of the two lists. All that gives is an invalid character error on the line. – Terri Feb 03 '16 at 23:30
  • "Just access the array elements." -- not very helpful. If I knew what was missing or how to "just access the elements" I would not have posted the question. – Terri Feb 03 '16 at 23:32
  • No '$.each() loop' in this code. Had it in my first version. Again, it's fairly easy to troubleshoot something I know exists. Knowing what's missing is another matter. – Terri Feb 03 '16 at 23:33
  • try [this](https://jsfiddle.net/p5fbh415/2/) – Siddharth Feb 04 '16 at 00:11
  • Siddharth... the link to fiddle returns a 404. – Terri Feb 04 '16 at 00:22
  • @Terri In the solution provided in related question `result` is JSON. You can use this variable anyway you need. It depends where you want to access it. Easy way to check it is define one global variable in JS and assign value of result to that variable inside `then` on related question solution. `var gloVar = {};` Inside `.then` on related question before JSON.stringify `gloVar = result;` Then you could access gloVar where ever you need as JSON. – pratikpawar Feb 04 '16 at 00:28
  • @Terri try [this](https://jsfiddle.net/nLk3x54y/) – Siddharth Feb 04 '16 at 00:42

1 Answers1

3

Terry, you wrote: "All that gives is an invalid character error on the line"? Then result is not a valid json. Test it here: http://jsonlint.com/, fix it, then try again.

var data = {
  d:{
    results: [
      { "Title": "apple", "Type": "rome", "State": null }, 
      { "Title": "grape", "Type": "fruit", "State": null }
    ]
  }
};

var title = '';
var type = '';
$.each(data.d.results, function (index, value) {
    title += "Title: " + value.Title + "<br/>";
    type += "Type: " + value.Type  + "<br/>";
}); 

alert(title + type);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Gavriel
  • 18,880
  • 12
  • 68
  • 105
  • Gavriel, that's it. The piece I was missing. The JSON returned correctly, but I was missing the part final step -- $.each() The following works (where "result" is the variable that holds the data) var title = ''; var type = ''; $.each(result, function (index, value) { title += "Title: " + value.Title + "
    "; type += "Type: " + value.Type + "
    "; });
    – Terri Feb 04 '16 at 14:49