1

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":[ "[email protected]","[email protected]"]}]}

And I want it to be:

{"instructor":[{"instructor_emails":{ "email":"[email protected]","email":"[email protected]"}}]}

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?

2
  • 1
    There cannot be two keys with the same name in an object. Commented Jul 18, 2016 at 8:50
  • yup your right...I actually already had it right with the above mention code I tried, it returns an array of objects containing the "email" : "[email protected]" I'm just trying to manipulate it wrong..thanks for pointing that out. I should have realized it from the get go Commented Jul 18, 2016 at 8:56

2 Answers 2

0

This was correct all along (Thanks Alex)

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

Returns:

{instructor:[instructor_emails[{"email":"[email protected]",{"email":"[email protected]"}]]}

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

0

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", ["[email protected]", "[email protected]"]),
    new Instructor("Michael Bennet II", ["[email protected]", "[email protected]"]),
];
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", ["[email protected]", "[email protected]"]),
	new Instructor("Michael Bennet II", ["[email protected]", "[email protected]"]),
];
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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.