Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

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?

share|improve this question
1  
There cannot be two keys with the same name in an object. – Alex M Jul 18 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 – OGJoshZero Jul 18 at 8:56
up vote 0 down vote accepted

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

share|improve this answer

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>

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.