![](/img/trans.png)
[英]Find key and update it's value and the next key value [Javascript Object]
[英]Update Javascript Object Value by Key/Value Reference
我有一个如下所示的javascript对象:
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(',') : '')
});
在页面上执行操作时,我需要更新此对象中departmentID
的值。
但是,我需要针对对象中的特定结果执行此操作。
因此,首先我需要在selectedUsers.qid = 'bob123'
处找到结果,然后手动设置其departmentID
的值。
为了编辑特定键的值,搜索此结果的最佳方法是什么?
是否可以将对象保存在“ selectedUsers”中,从而使“ qid”成为其键? 我假设 qid是唯一值。
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(',') : '')
}
那你要做的就是
selectedUsers['bob123'].departmentID = yourNewValue;
如果不...
然后,您只需要循环遍历整个数组(selectedUsers),直到找到qid与您的搜索匹配的数组。
for (var index=0, limit=selectedUsers.length; index < limit; ++index) {
if(selectedUsers[index].qid == "bob123"){
selectedUsers[index].deparmentID = "yourvalue";
break;
}
}
我假设这都是在客户端上发生的,而不是在某些数据存储区中发生的。 在那种情况下,我将查看lodash.js库-特别是find
方法:
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'
所以你的情况是
var user = _.find(selectedUsers, function (user) {user.qid === searchId; });
user.departmentID = someOtherId;
// whatever else you need to do
用户underscore.js 查找功能
例:
var user = _.find(selectedUsers, function (item) { return item.departmentID === deartmentID});
user.departmentID = yourNewValue;
假设selectedUsers
是一个数组,则可以执行以下操作:
function setSelectedUsersDepartment(qid, departmentID) {
for (var i in selectedUsers) {
var user = selectedUsers[i];
if (user.qid === qid) {
user.departmentID = departmentID;
}
}
}
或者,如果可以使用ES6 Array.find函数(不支持IE):
function setSelectedUsersDepartment (qid, newDepartmentID) {
var user = selectedUsers.find(user => user.qid === qid);
if (user !== undefined) {
user.departmentID = newDepartmentID;
}
}
如果要为bob设置departmentID,请调用以下函数
setSelectedUsersDepartment('bob123', 'new_departmentID');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.