0

I am currently using

rows.length

in JS and it's giving me the number of rows as output.

The actual code is like this

oResources.html("<h3>Number of Rows = "+ rows.length+"</h3>");

The output is Number of Rows = 1

But I want to see the values of the rows not the number of rows.

The whole function looks like below after editing:

 this.get_resources = function(rows)
    {   var oResources = $('#resources-'+ options.plugin_uuid);
        //oResources.html("<h3>Number of Rows = "+ rows.length+"</h3>");
        //oResources.html("<h3>Content of Rows = "+ rows.join(' :: ')+"</h3>");
        var data = "";                      // Temporary `data` variable
        for(var key in rows) {              // Loop through the rows
            if(rows.hasOwnProperty(key)){
                data += rows[key] + "<br />\n"; // Add the current row to the data
                }
        }
        oResources.html(data);              // Display the data
        //console.log(rows.length);
    };

Now it's printing out [object] [object]

My question is:

The output is supposed to be an email id instead of [object] [object]. How to solve this?

5
  • So rows is an array? What values in that array do you want printed? Commented Feb 15, 2013 at 14:38
  • I want the real value of the array to be printed. For example in rows i have email addresses. I want that email address to be printed Commented Feb 15, 2013 at 14:42
  • so... document.write(rows[0])? Commented Feb 15, 2013 at 14:42
  • It's printing [object] [object]. When I use rows.length it displays: Number of Rows = 1 Commented Feb 15, 2013 at 14:46
  • 1
    so you've got an object, which means we can't help you unless you explain exactly what's IN this object. not just a vague "email addresses". maybe that's an endpoint datum in the object, but objects can have artbitrary structures. Commented Feb 15, 2013 at 14:47

4 Answers 4

2

Something like that?

oResources.html("<h3>Content of Rows = "+ rows.join(' :: ')+"</h3>");

EDIT: As your data format seems to have another level of depth, I've made a fiddle to give some insight on what you could do. http://jsfiddle.net/TyeQE/

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

4 Comments

I like that, it's the equivalent of PHP implode() function.
it's printing Content of Rows = [object Object]. the actual value is an email address
Well I don't know the format of your data, but in this case you would want to loop through the array as the other answers suggest and access each objects values, depending on what you need.
I just cobbled together this fiddle: jsfiddle.net/TyeQE Do a console.log of your rows array to see the actual format of your array, so you can adjust the code in the fiddle according to your needs.
1

try this:

var data = "";                      // Temporary `data` variable
for(var key in rows) {              // Loop through the rows
    if(rows.hasOwnProperty(key)){
        data += rows[key] + "<br />\n"; // Add the current row to the data
    }
}
oResources.html(data);              // Display the data

4 Comments

it's printing [object Object] function (){return v; } function Array() { [native code] } function pop() { [native code] } function push() { [native code] } function reverse() { [native code] } function shift() { [native code] } function sort() { [native code] } function splice() { [native code] }
Yes now all the garbage values are gone. It's printing: [object Object]. The real value is an email address
Apparently you have an object in your array?
Yes they are objects. I am editing my question for better understanding
1

Yes, 'rows' looks like an array. You need a for loop to go over the array and print it out.

var str = ""
for(var i = 0; i < rows.length; i++) {
  str = rows[i] + "<br>";
}
oResources.html($str);

1 Comment

That will result in only the last row being displayed, since you're overwriting the .html() each time.
0

Try console.log(rows). Open console (usualy F12) in the browser to see the result.

3 Comments

With Firefox you'll need firebug plugin !
Yes I can see the row objects in console. But I want to display it in the page
Newer Firefox version have their own developer tools. Check CTRL+Shift+K

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.