admin管理员组文章数量:1208155
I need convert arrays inside my parent array to objects to match my database model data.
I have array like this:
emails: Array[2]
0: "[email protected]"
1: "[email protected]"
id: 1
firstname: "Jane"
lastname: "Doe
What I want to achieve is to convert emails array to array of objects like this:
emails: Array[2]
0:
{
name: "[email protected]"
}
1:
{
name: "[email protected]"
}
id: 1
firstname: "Jane"
lastname: "Doe
I tried to use this code to convert array to object but for some reason it fails (no data are displayed -> variable rv is empty):
var rv = {};
for (var i = 0; i < dbInfo.emails.length; ++i)
if (dbInfo.emails[i] !== undefined) rv[i] = dbInfo.emails[i];
Does someone knows why my code fails and does someone knows solution for this type of problem?
Thanks advance.
I need convert arrays inside my parent array to objects to match my database model data.
I have array like this:
emails: Array[2]
0: "[email protected]"
1: "[email protected]"
id: 1
firstname: "Jane"
lastname: "Doe
What I want to achieve is to convert emails array to array of objects like this:
emails: Array[2]
0:
{
name: "[email protected]"
}
1:
{
name: "[email protected]"
}
id: 1
firstname: "Jane"
lastname: "Doe
I tried to use this code to convert array to object but for some reason it fails (no data are displayed -> variable rv is empty):
var rv = {};
for (var i = 0; i < dbInfo.emails.length; ++i)
if (dbInfo.emails[i] !== undefined) rv[i] = dbInfo.emails[i];
Does someone knows why my code fails and does someone knows solution for this type of problem?
Thanks advance.
Share Improve this question asked Dec 29, 2014 at 19:34 jureisprojureispro 1,4026 gold badges25 silver badges46 bronze badges 4 |4 Answers
Reset to default 10This is a perfect use for the Array.prototype.map
function:
dbInfo.emails = dbInfo.emails.map(function(e) {
return { name: e };
});
i.e. just convert each individual element of the array (e
) into an object { name: email }
I think what are you looking for was angular.extend. For a good article about angular.extend click here. For documentation click here .
var newObj = {};
angular.extend(newObj,[Array here]);
You are putting the emails into an object. Instead, you want to wrap each email in its own object, and put it back in the array.
for (var i = 0; i < dbInfo.emails.length; ++i) {
if(dbInfo.emails[i] !== undefined) {
dbInfo.emails[i] = { name: dbInfo.emails[i] };
}
}
this work perfectly //Option Select
var range = [];
for (var i=1; i<$scope.totalItems+1; i++) {
range.push({value:i});
}
console.log(range);
本文标签: Convert array to objects using JavaScriptAngularJSStack Overflow
版权声明:本文标题:Convert array to objects using JavaScriptAngularJS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738707544a2108004.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
rv
being empty, unlessdbInfo.emails
is empty. I just tested it. – forgivenson Commented Dec 29, 2014 at 19:47id
,firstname
andlastname
properties are fine, but you should have anemail
property that is an array, containing the multiple email addresses. – Jonathan M Commented Dec 29, 2014 at 19:58