[英]Update the value of a field in a JSON object using Jquery
我有一个JSON对象:
[{"file":"post_files/feb641027629b6a7ba78fb47063c6763vid.flv","title":"Sample"},
{"file":"post_files/74c73cf0e28d6ca431ab4c2e2299f39fbarsandtone.flv","title":"Bars"}]
如您所见, file
字段具有诸如post_file/feb.................flv
。
我想要做的就是将http://divinotech.in/
附加到所有file
字段的值,即该值应像这样进行编辑:
http://divinotech.in/post_file/feb.................flv
我怎样才能做到这一点?
您可以遍历数组的元素并修改file
属性:
var array = [
{ "file": "post_files/feb641027629b6a7ba78fb47063c6763vid.flv", "title":"Sample"},
{ "file": "post_files/74c73cf0e28d6ca431ab4c2e2299f39fbarsandtone.flv", "title":"Bars"}
];
for (var i = 0; i < array.length; i++) {
array[i].file = 'http://divinotech.in/' + array[i].file;
}
// at this stage the array will contain the desired values
这是一个live demo
。
尝试这个
var result=[{"file":"post_files/feb641027629b6a7ba78fb47063c6763vid.flv","title":"Sample"},
{"file":"post_files/74c73cf0e28d6ca431ab4c2e2299f39fbarsandtone.flv","title":"Bars"}]
for (var i in result) {
$("body").append('<a href="http://divinotech.in/'+result[i].file+'">'+result[i].title+'</a></br>');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.