繁体   English   中英

如何将数组转换为数组中的对象 - Angular和Javascript

[英]How do I convert an array into an object within array - Angular and Javascript

我想要做的是在一个对象内的数组内的一个对象中取一个数组(我知道我的数据结构很荒谬,任何帮助都会很好)并将最后一个数组转换为一个带有“键”的对象:“值”。 我正在使用Angular 1.5.7,如果Angular中有任何东西可以帮助做到这一点。 我知道这可能毫无意义。 我无法想办法说出我想要做的事情,所以让我告诉你:

我从这样的对象开始:

{"instructor":[{"instructor_emails":[ "test@test.com","tester@tester.com"]}]}

我希望它是:

{"instructor":[{"instructor_emails":{ "email":"test@test.com","email":"tester@tester.com"}}]}

我尝试了几件事,我发现最接近的是:

instructor.instructor_emails.map(function(e) {
        return { email: e };
});

但它并没有完全做我想做的事......有什么想法吗?

这一直都是正确的(谢谢Alex)

instructor.instructor_emails.map(function(e) {
    return { email: e };
});

返回:

{instructor:[instructor_emails[{"email":"example1@example1.com",{"email":"example1@example1.com"}]]}

数据结构仍然很荒谬,但它足以满足我的目的

您应该阅读面向对象的编程以获得最佳数据存储,尤其是有关类的存储。 要在传统的OOP语言(如Java)之间转换为JavaScript,您可以使用TypeScript

下面是我使用TypeScript创建的片段:

 /// <reference path="definitions/jquery.d.ts" /> console.clear(); var Instructor = (function () { function Instructor(name, emails) { if (name === void 0) { name = ""; } if (emails === void 0) { emails = []; } this.name = name; this.emails = emails; } Instructor.prototype.addEmail = function (email) { if (email === void 0) { email = ""; } //Run validation if (email.length > 3) { this.emails.push(email); } }; Instructor.prototype.getEmails = function (type) { if (type === void 0) { type = "array"; } var self = this; type = type.toLowerCase(); var getEmails = { string: function () { return self.emails.join(" "); }, object: function () { return self.emails.map(function (e) { return { email: e }; }); } }; if (getEmails[type] === void 0) { return this.emails; } else { return getEmails[type](); } }; return Instructor; }()); var instructors = [ new Instructor("Michael Bennet I", ["test@test.com", "tester@tester.com"]), new Instructor("Michael Bennet II", ["test@test.com", "tester@tester.com"]), ]; console.log('array', instructors[0].getEmails()); console.log('object', instructors[0].getEmails("object")); console.log('string', instructors[0].getEmails("String")); 
 /* // This is TypeScript class Instructor { constructor(public name: string = "", public emails: string[] = []) { } public addEmail(email: string = "") { //Run validation if (email.length > 3) { this.emails.push(email); } } public getEmails(type: string = "array") { var self = this; type = type.toLowerCase(); var getEmails = { string: function () { return self.emails.join(" "); }, object: function () { return self.emails.map(function (e) { return { email: e }; }); } } if (getEmails[type] === void 0) { return this.emails; } else { return getEmails[type](); } } } var instructors: Instructor[] = [ new Instructor("Michael Bennet I", ["test@test.com", "tester@tester.com"]), new Instructor("Michael Bennet II", ["test@test.com", "tester@tester.com"]), ]; console.log('array',instructors[0].getEmails()); console.log('object',instructors[0].getEmails("object")); console.log('string',instructors[0].getEmails("String")); 
 <p>Object can have their own functions to "get" data they contain in unique ways.</p> <p>This way, you can get the same data in several different ways</p> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM