22

I want to store information like:

Pseudo-Code

array(manager) = {"Prateek","Rudresh","Prashant"};
array(employee) = {"namit","amit","sushil"};
array(hr) = {"priya","seema","nakul"};

What kind of data structure can I use?

1
  • Not sure I follow, why can't you use a javascript array like : var managers = ["Prateek","Rudresh","Prashant"]; ? Commented Jul 21, 2011 at 5:51

8 Answers 8

39

You can use arrays to store list of data ; and objects for key-value


In you case, you'd probably use both :

var data = {
    'manager': ["Prateek","Rudresh","Prashant"], 
    'employee': ["namit","amit","sushil"], 
    'hr': ["priya","seema","nakul"]
};

Here, data is an object ; which contains three arrays.

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

Comments

13

An object:

var myobj = {
  "manager": ["Prateek","Rudresh","Prashant"],
  "employee": ["namit","amit","sushil"],
  "hr": ["priya","seema","nakul"]
}

alert(myobj['employee'][1]); // Outputs "amit"

3 Comments

Yes i think i was looking for it.
Did you mean for {"Prateek","Rudresh","Prashant"} to be an array?
@jfriend00 Yes, I think he is wanting to use them as arrays.
10

A normal object will do:

var a = {
    key1: "value1",
    key2: ["value2.1","value2.2"]
    /*etc*/
}

Access with:

a.key1
a["key1"]

Comments

8

With ES2015/ES6 you have Map type.

Using Map your code will look like

const map = new Map([
  ['manager', ['Prateek', 'Rudresh', 'Prashant']],
  ['employee', ['namit', 'amit', 'sushil']],
  ['hr', ['priya', 'seema', 'nakul']]
])

console.log(...map.entries())

To get Individual value you can use Map.get('key') method

Comments

7

you could store them in an array of objects:

    var Staff = [
       { name: 'Prateek', role: manager },
       { name: 'Rudresh', role: manager },
       { name: 'Prashant', role: manager },
       { name: 'Namit', role: employee },
       { name: 'Amit', role: employee },
       { name: 'Sushil', role: employee },
       { name: 'Priya', role: hr },
       { name: 'Seema', role: hr },
       { name: 'Nakul', role: hr },
    ];

adding an ID attribute might be useful too depending on your application. i.e

    { id: 223, name: 'Prateek', role: manager },

Comments

2

Or use JSON like this. A little change of your pseudo code, but it will be serchable and extendable.

var Person = [
{
    "name": "Prateek",
    "position": "manager"},
{
    "name": "James",
    "position": "employee"}

];

Comments

1

Yes there is:

var theArray = {};
theArray["manager"] = ["Prateek","Rudresh","Prashant"];
theArray["employee"] = ["namit","amit","sushil"];
theArray["hr"] = ["priya","seema","nakul"];

5 Comments

This is not correct, any object is that structure. Your example works because an array is just an object.
@Emil Ivanov: This is perfectly correct, it create a 2 dimensional array. please elaborate which part is incorrect
Arrays in Javascript can only be indexed by a number. Indexing them by string is possible, because any object in js can be indexed by a string.
i am very confused, so which part of my answer is wrong again?
@lbu - Run "JSON.stringify(theArray);" you will see that it returns an object containing 3 properties, which are arrays. I think Emil's complaint is that naming an object "theArray" is misleading, it is helpful to understand theArray correctly as an object, not an array. Yes your code works, but "2 dimensional array" concept is incorrect.
-1

Even you can use stuff as below :-

var obj = new Object();

obj.name = 'Jatin';
obj.place = 'Delhi';

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.