Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I want to display the title and url from the array, it displays the value but as [object object] when i am debugging, i want it to display the text value and not only as objects.

here is my code

C#
function successHandler(data) {

        var jsonObject = JSON.parse(data.body);
        var dataArr = $.makeArray(jsonObject.d.results);
        var resultArr = $.map(dataArr, function (item) {



            return { label: item.Title, value: item.Url };


        });


UPDATE
========================

Unable to get property length?

function successHandler(data) {
   
        var jsonObject = JSON.parse(data.body);
        var dataArr = $.makeArray(jsonObject.d.results);
        var resultArr = $.map(dataArr, function (item) {  
            
            $.each(resultArr, function (key, value) {

                $("#result").text(value[key].label + " " + value[key].value);
               
            });
         
            
          
            
        });

        var resultDisplay = document.getElementById("result");
        resultDisplay.innerHtml = resultArr;


}


New UPDATE
====
Error: Unable to get property 'length' of undefined or null reference

C#
function successHandler(data) {

    var jsonObject = JSON.parse(data.body);
    var dataArr = $.makeArray(jsonObject.d.results);
    var resultArr = $.map(dataArr, function () {
        $.each(resultArr, function (key, value) {


        })
        var str = '';
        str += (value.label + " " + value.value);
        $("#result").text(str);



    });


}
Posted
Updated 12-Jan-15 23:51pm
v3

1 solution

what you are returning from map function is an object (simple JSON), instead try using resultArr[0].label & resultArr[0].value

Ex:
JavaScript
resultDisplay.innerHtml = resultArr[0].label + " " + resultArr[0].value;
 
Share this answer
 
Comments
Kurac1 13-Jan-15 4:06am    
Doing like that it only displays data one time second time it wonk work
SujayC 13-Jan-15 5:15am    
$("#result").text(value[key].label + " " + value[key].value);
try changing the above line of code to
var str = ''; //this statement should be out of each block
str += value.label + " " + value.value);
$("#result").text(str);
value in the each loop will itself be individual json object from the array. Further

Further, there seems to be some syntax/code formatting error in your updated snippet, hope you have corrected that as well.
Kurac1 13-Jan-15 4:09am    
I have updated
Kurac1 13-Jan-15 5:50am    
i will update again, it dont work
SujayC 13-Jan-15 11:05am    
sorry if i wasn't clear in my previous comment what i meant was the following

function successHandler(data) {
var jsonObject = JSON.parse(data.body);
var dataArr = $.makeArray(jsonObject.d.results);
var resultArr = $.map(dataArr, function (item) {
return { 'label' : item.Title, 'value' : item.Url }
});
var str = '';
$.each(resultArr, function (key, value) {
str += (value.label + " " + value.value);
});
$("#result").text(str);
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900