简体   繁体   中英

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

What I am trying to do is take an array within an object within an array within an object (I know my data structure is ridiculous, any help with that would be great also) and convert the last array into an object with a "key":"value". I'm using Angular 1.5.7 if there is anything in Angular that could help do this. I know that probably makes no sense. I couldn't figure out a way to say what I am trying to do clearly so let me show you:

I start with an object like this:

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

And I want it to be:

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

I tried a couple of things and the closest I found was:

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

But it doesn't quite do what I'm trying to do... Any thoughts?

This was correct all along (Thanks Alex)

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

Returns:

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

The data structure is still ridiculous but it will suffice for what I am trying to do

You should read up on Object-oriented programming for optimal data storage, particularly concerning classes . To transition between traditional OOP languages, like Java, to JavaScript you can use TypeScript .

Below is a snippet i created using 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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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