2

Very simple question I guess but not sure what's the best approach to achieve this.

I want to create an object of multiple projects. I should do something like this:

var projects = {};
projects.url = "www.example.com";
projects.description = "Project Description";

But how do I use this projects object for more than one project?

Edit: I though about using arrays as some of the answers suggest but is there any way to avoid arrays?

7
  • Why don't you want to use an array? Commented Feb 23, 2013 at 18:46
  • 4
    Why would you want to avoid arrays? Whenever you have multiple somethings, arrays/lists/etc are a natural way to hold them. Commented Feb 23, 2013 at 18:46
  • I read somewhere that arrays are more expensive than objects so I was just wondering. Commented Feb 23, 2013 at 18:48
  • 3
    @RKour Seriously! Just use an array! They fit your purpose better than objects would. Commented Feb 23, 2013 at 18:51
  • You are going to the wrong shop if they charge you more for arrays than objects. Actually, arrays are just types of objects, and not going to be the bottleneck in whatever you are developing. Commented Feb 23, 2013 at 18:51

3 Answers 3

4

Have an array of project objects.

var projects = [
    {
        url: 'http://www.example.com',
        description: 'Project Description'
    },
    {
        url: 'http://www.example2.com',
        description: 'Project 2'
    }
];
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Brad - Is there any possibility to avoid arrays?
@RKour, Why would you want to avoid an array? An array is exactly what you want...
1

Create a project class:

var Project = function() {};

Project.prototype = {
    url: "www.example.com",
    description: "Project Description"
};

Then you can create multiple instances of these objects:

var proj1 = new Project(),
    proj2 = new Project();

1 Comment

Very good alternative if you must not use arrays (could be a trick question ) but Brad's answer serves my purpose better. +1.
-1

The best answer, is to use an array, but if you insist on trying to hammer a square peg into a round hole, you can use objects like this...

DISCLAIMER: This code is ugly as your mother in law, don't use it

var projects = {}

projects.project1 = {
  url = "www.example.com";
  description = "Project Description";
}

projects.project2 = {
  url = "www.example2.com";
  description = "Project2 Description";
}

And select like this...

for(i=0;i<2;i++){
  console.log(projects["project"+i].description)
}

EDIT: Some potentially useful code...

The only time I can imagine it being useful to use objects instead of arrays for something like this, is if your projects have unique ID's and you need to look them up a lot out of sequence. There is an advantage in listing the projects, using their ID as the key, so that if you know the ID, you can select the object directly, rather than looping through an array to find the one you want...

projects = {
  projAcHRx:{ ... },
  projFhv5R:{ ... }
}

// select project: projAcHRx
projects.projAcHRx // <~ very simple lookup if you know the ID

Rather than

projects = [
  { id:'projAcHRx',
    ... },
  { id:'projFhv5R',
    ... },
]

// select project: projAcHRx
for(i=projects.length;i;i--){
  if(projects[i] == 'projAcHRx'){
    var selected = projects[i] // <~ hideously complex selection!
  }
}

4 Comments

Thanks Billy - I afraid it's not very neat. I'll go with arrays instead!
Please, don't even suggest this. Someone will inevitably come by this question in the future and copy and paste it into their project.
Second what Brad said - if you have to put that kind of warning in the front of it, it's not worth sharing.
I disagree, I think the forum is a place for discussion, so hopefully people will read the comments etc... before copy/pasting. If people are going to drive by and randomly copy/paste code without reading the context, their work is doomed to be sloppy, and not our responsibility to improve. On a related note, I discovered there is a video sharing service (I think vine) which limits you to 6 second videos - what is the world coming to. I used to read books to find out this stuff - not copy/paste without reading a paragraph!

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.