admin管理员组文章数量:1287651
I currently am dealing with a Web Service that is returning an array of strings to the client. From here I would like to take this array of strings and convert it into an object that gives each string a name so to reference it later.
So start with this:
var result = ["test", "hello", "goodbye"];
And I would like to end up with this:
var final = [{'value': "test"}, {'value': "hello"}, {'value': "goodbye"}];
I use jquery. Is there a easy to acplish this?
I currently am dealing with a Web Service that is returning an array of strings to the client. From here I would like to take this array of strings and convert it into an object that gives each string a name so to reference it later.
So start with this:
var result = ["test", "hello", "goodbye"];
And I would like to end up with this:
var final = [{'value': "test"}, {'value': "hello"}, {'value': "goodbye"}];
I use jquery. Is there a easy to acplish this?
Share Improve this question asked May 20, 2011 at 16:02 Collin EstesCollin Estes 5,7997 gold badges53 silver badges71 bronze badges4 Answers
Reset to default 7var final = $.map(result, function(val) {
return { value: val };
});
Alternatively you can use the ES5 alternative
var final result.map(function(val) {
return { value: val };
});
Or a simple iteration.
var final = [];
for (var i = 0, ii = result.length; i < ii; i++) {
final.push({ value: result[i] });
}
I don't think jQuery has to be used here.
var result = ["test", "hello", "goodbye"];
var final = [];
for(var i = 0; i < result.length; i++) {
final.push({value: result[i]})
}
I haven't tested this but you can do something like
$(result).map(function(){return {'value':this}}
);
You could do something like the following:
var input = ["one", "two", "three"],
output = [],
obj;
for (var i = 0; i < input.length; i++)
{
obj = { "value" : input[i] };
output.push(obj);
}
Link to the fiddle
本文标签: jqueryConverting string array to NameValue object in javascriptStack Overflow
版权声明:本文标题:jquery - Converting string array to NameValue object in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741253373a2366207.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论