簡體   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