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

时间:2016-07-18 08:43:14

标签: javascript arrays angularjs javascript-objects

我要做的是在一个对象内的数组中的一个对象中取一个数组(我知道我的数据结构很荒谬,任何帮助都会很好)并将最后一个数组转换为一个对象“核心价值”。我正在使用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 };
});

但它并没有完全按照我的意图去做......有什么想法吗?

2 个答案:

答案 0 :(得分:0)

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

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

返回:

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

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

答案 1 :(得分:0)

您应该阅读Object-oriented programming以获得最佳数据存储,尤其是classes。要将传统的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>