0

How do I convert testObj to an array?

function MinEvent(event, width, left){
  this.start = event.start;
  this.end = event.end;
  this.width = width;
  this.top = event.start;
  this.left = left;
}

var testObj = {};
for (var j=0; j<eventLists.length; j++) {
  var eve = eventLists[j];
  var event = new MinEvent(eventList[j], width, left);
  testObj[eve.id] = event;
}
5
  • All JavaScript objects are basically associative arrays. Your code as written should work (Assuming new Event is supposed to be new MinEvent and eventLists is initialized to something sane). Commented Dec 22, 2009 at 20:36
  • 2
    If you want testObj to be a true array, you need to tell us how you want its properties converted. Maybe it would help if you tell us what you're trying to do with it? Commented Dec 22, 2009 at 20:37
  • If I am not wrong, if I keep testObj as an object instead of a true array, i wouldn't know it's length. Right? Commented Dec 22, 2009 at 20:45
  • @priyank.mp: right. However if you simply switch {} to [] you cannot rely on .length either: string keys don't affect .length, and numeric keys simply bump .length to the highest index +1. In other words, you should answer @Douglas' question... Commented Dec 22, 2009 at 21:02
  • Once I get testObj, I will go through each element, and then I will generate some html code based on the values - start, end, width,top and left.Ex: <div style="top:val1;left:val2;width:val3">test</div>. I will have to sort this array based on start value(testObj[id].start) before I generate an html. Commented Dec 22, 2009 at 21:26

4 Answers 4

1

Technically every object in javascript can be treated like an array. Your code should work as-is.

But either way, looking at your code, why not define testObj as var testObj = [];?

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

Comments

0

If you declare testObj as an array initially you don't need to convert it.

//declare the array
var testObj = new Array();

or

//declare the array
var testObj = [];

Index based access should work for either of these. I.e.

testObj[0] = "someval";

etc.

1 Comment

LOL why people downed every answer to this question is beyond me. They're all valid, including mine. It's a simple javascript array. It's like grade school around here. Oh me! me! me! I had my hand up first ;)
0

I'm not sure why you'd want to do that. JavaScript objects are actually the same as an associative array already. You could access all of its properties and methods like this:

for(prop in obj) {
    alert(prop + ” value : ”+ obj[prop];
}

Maybe it would help if you told us what you wanted to do once you converted the object inot an array (and what you want the result to look like).

Comments

0
var array = []
for( var prop in testObj )
{
    array.push( testObj[prop] );
}

Is this what you want?

Comments

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.