-1

i have 2 arrays. One with users and other with user's manager

var user=[a,b,c,d,e];
var manager=[x,y,z]

how to associate users to respective managers if the manager is same for users

manager  user
x        a,b
y         c
z        d,e

i'm looking to achieve in javascipt.

DontVoteMeDown
  • 20,534
  • 10
  • 70
  • 102

2 Answers2

0

That's easy you can do it by

var manager_user = [
    {
        "mgr": "x",
        "users": [
            "a",
            "b",
            "e"
        ]
    }
];

And it's a validated json!

Update: Based on yr comment you can use functions and do object literals:

var Manager_User = function(mgr, usrs) {

    return {
        manager: mgr,
        usrs: usrs,
        addUser: function(mgr, usr) {
            usrs.push(usr);
        },
        removeUser: function(index) {
            usrs.splice(index, 1);
        }
    }
}

var mgrUser = new Manager_User("x", ["a", "b", "c"]);

I've uploaded some of my js examples on github, you can have a look, especially calc.js

Daniel B
  • 2,977
  • 2
  • 31
  • 40
0

you can keep key value pair. key as manager.value as a array of {a,b}.

var obj = {
key1: value1,
key2: value2
};

obj["key3"] = "value3";

obj["x"] = {"a","b"};

How can I add a key/value pair to a JavaScript object?

Sanka
  • 1,194
  • 1
  • 10
  • 19