How do I convert the this object to an array.
Please help me, many thanks.

Recommended Answers

All 3 Replies

It can already act like an array
Dot Operator

var someObject = {
  someProp : 9,
  someFunc : function () { return this.someProp; }
};
alert(someObject.someFunc()); // 9

Array Accessor

var someObject = {
  someProp : 9,
  someFunc : function () { return this['someProp']; }
};
alert(someObject.someFunc()); // 9

Both act exactly the same.

That's really not enough information. What is this representing? Do you have some code to look at and maybe a better description of why you would want to create an array from the this object?

Basically I'm trying to do chaining. In order to chain I need to return this. But I need it to return an array of DOM objects or variables so that I can cache them. The only way I can access the this element is when I add the index at the end. What I know is that jQuery managed to do it. And there is something in the code that goes like this:

setArray: function(elems){
this.length = 0
Array.prototype.push.apply(this, elems)
return this
}

And this function is linked to the end of another function like this:

return this.setArray(....)


Thanks for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.