0

I have a javascript object that looks like below:

selectedUsers.push({
   qid   : qid,
   first : $(this).data('emp_first'),
   last  : $(this).data('emp_last'),
   department : $(this).data('emp_dept'),
   ntid : $(this).data('emp_ntid'),
   supFirst : $(this).data('emp_sup_first'),
   supLast : $(this).data('emp_sup_last'),
   supNTID : $(this).data('emp_sup_ntid'),
   title : ($(this).data('emp_title') ? $(this).data('emp_title') : 'N/A'),
   location: ($(this).data('emp_location') ? $(this).data('emp_location') : 'N/A'),
   skillset : ($(this).data('emp_skillset') ? $(this).data('emp_skillset') : 'N/A'),
   departmentID : $(this).data('emp_dept_id'),
   titleID : $(this).data('emp_title_id'),
   regionID : $(this).data('emp_region_id'),
   instances : ($(this).data('emp_instances') != '' ? $(this).data('emp_instances').split(',') : '')
});

When an action is taken on my page, I need to update the value of departmentID in this object.

However, I need to do this for a specific result in the object.

So first I need to find the result where selectedUsers.qid = 'bob123' and then set the value for his departmentID manually.

What would be the best way to search for this result in order to edit the value for the specific key?

4 Answers 4

4

Is it possible to save objects in your "selectedUsers" so that "qid" is their key? I am assuming that qid are unique values.

selectedUsers = {};
selectedUsers[qid] = {
   first : $(this).data('emp_first'),
   last  : $(this).data('emp_last'),
   department : $(this).data('emp_dept'),
   ntid : $(this).data('emp_ntid'),
   supFirst : $(this).data('emp_sup_first'),
   supLast : $(this).data('emp_sup_last'),
   supNTID : $(this).data('emp_sup_ntid'),
   title : ($(this).data('emp_title') ? $(this).data('emp_title') : 'N/A'),
   location: ($(this).data('emp_location') ? $(this).data('emp_location') : 'N/A'),
   skillset : ($(this).data('emp_skillset') ? $(this).data('emp_skillset') : 'N/A'),
   departmentID : $(this).data('emp_dept_id'),
   titleID : $(this).data('emp_title_id'),
   regionID : $(this).data('emp_region_id'),
   instances : ($(this).data('emp_instances') != '' ? $(this).data('emp_instances').split(',') : '')
}

Then all you would have to do is

selectedUsers['bob123'].departmentID = yourNewValue;

IF NOT...

Then you will just have to loop the entire array (selectedUsers) until you find the one where qid matches your search.

for (var index=0, limit=selectedUsers.length; index < limit; ++index) {
    if(selectedUsers[index].qid == "bob123"){
      selectedUsers[index].deparmentID = "yourvalue";  
      break;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

I'm assuming this is all happening on the client and not in some datastore. In that case I would look into the lodash.js library- specifically the find method:

var users = [
  { 'user': 'barney',  'age': 36, 'active': true },
  { 'user': 'fred',    'age': 40, 'active': false },
  { 'user': 'pebbles', 'age': 1,  'active': true }
];

_.find(users, function(o) { return o.age < 40; });
// → 

object for 'barney'

so in your case you would have something like

var user = _.find(selectedUsers, function (user) {user.qid === searchId; });
user.departmentID = someOtherId;
// whatever else you need to do

Comments

0

User underscore.js find Function

example:
var user = _.find(selectedUsers, function (item) { return item.departmentID === deartmentID});
user.departmentID = yourNewValue;

Comments

0

Assuming that selectedUsers is an array, you could do something like this:

function setSelectedUsersDepartment(qid, departmentID) {
    for (var i in selectedUsers) {
        var user = selectedUsers[i];
        if (user.qid === qid) {
            user.departmentID = departmentID;
        }
    }
}

Or if you can use the ES6 Array.find function (no IE support):

function setSelectedUsersDepartment (qid, newDepartmentID) {
    var user = selectedUsers.find(user => user.qid === qid);
    if (user !== undefined) {
        user.departmentID = newDepartmentID;
    }
}

if you want to set the departmentID for bob, you call the function like this

setSelectedUsersDepartment('bob123', 'new_departmentID');

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.