【问题标题】:How do I convert an array into an object within array - Angular and Javascript如何将数组转换为数组内的对象 - Angular 和 Javascript
【发布时间】:2016-07-18 08:43:14
【问题描述】:

我正在尝试做的是在对象中的数组中获取对象中的数组(我知道我的数据结构很荒谬,对此的任何帮助也会很棒)并将最后一个数组转换为具有“核心价值”。如果 Angular 中有任何东西可以帮助做到这一点,我正在使用 Angular 1.5.7。我知道这可能没有任何意义。我想不出一种方法来清楚地说出我想要做什么,所以让我告诉你:

我从这样一个对象开始:

{"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 };
});

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

【问题讨论】:

  • 一个对象中不能有两个同名的键。
  • 是的,你的权利......我实际上已经用上面提到的代码做了正确的尝试,它返回一个包含"email" : "example@example.com"的对象数组我只是试图错误地操作它..感谢您指出了这一点。我应该从一开始就意识到这一点

标签: javascript arrays angularjs javascript-objects


【解决方案1】:

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

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

返回:

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

数据结构仍然很荒谬,但对于我正在尝试做的事情已经足够了

【讨论】:

    【解决方案2】:

    您应该阅读Object-oriented programming 以获得最佳数据存储,尤其是关于classes。要在传统的 OOP 语言(如 Java)和 JavaScript 之间转换,您可以使用 TypeScript

    下面是我使用 TypeScript 创建的 sn-p:

    /// <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>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-22
      • 2022-07-09
      • 2021-01-07
      • 1970-01-01
      • 2015-01-13
      • 2011-04-21
      • 2019-05-17
      相关资源
      最近更新 更多