如何改变对象的值,它是使用JavaScript或jQuery的数组里面呢? [英] How to change value of object which is inside an array using javascript or jquery?
本文介绍了如何改变对象的值,它是使用JavaScript或jQuery的数组里面呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这些code来自jQuery用户界面自动完成:
these code come from jquery ui autocomplete:
var projects = [
{
value: "jquery",
label: "jQuery",
desc: "the write less, do more, JavaScript library",
icon: "jquery_32x32.png"
},
{
value: "jquery-ui",
label: "jQuery UI",
desc: "the official user interface library for jQuery",
icon: "jqueryui_32x32.png"
},
{
value: "sizzlejs",
label: "Sizzle JS",
desc: "a pure-JavaScript CSS selector engine",
icon: "sizzlejs_32x32.png"
}
];
例如,当我想改变的jQuery的UI的递减值,我怎么能比?做
for example,when I want to change the desc value of jquery-ui,how can I do than??
非常感谢你!
有没有更快的方法来获得数据?我的意思是给对象的名称,以获取其data.Just喜欢里面array.So对象,我可以以这种方式使用:jQuery的-ui.jquery-ui.desc = ....
推荐答案
您有像在数组中搜索:
function changeDesc( value, desc ) {
for (var i in projects) {
if (projects[i].value == value) {
projects[i].desc = desc;
break; //Stop this loop, we found it!
}
}
}
和使用它像
var projects = [ ... ];
changeDesc ( 'jquery-ui', 'new description' );
更新:
要更快地得到它:
var projects = {
jqueryUi : {
value: 'lol1',
desc: 'lol2'
}
};
projects.jqueryUi.desc = 'new string';
(在根据弗雷德里克的评论,你不应该在对象键使用连字符,或者你应该使用的jQuery-UI和项目[的jQuery-UI]符号。)
(In according to Frédéric's comment you shouldn't use hyphen in the object key, or you should use "jquery-ui" and projects["jquery-ui"] notation.)
这篇关于如何改变对象的值,它是使用JavaScript或jQuery的数组里面呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文