1

I have a var named onversation that contains this:

{
    "conversationId": "adbabc54-3308-436d-a48b-932f4010d3c6",
    "participantId": "415651e6-f0a5-4203-8019-4f88c3ed9cd5"
}

I also have an object named person that contains this:

{
    firstname: "fred", 
    surname: "smith", 
    age: "21", 
    gender: "male"
}

What I'd like is to have a combined object called result that looks like this

result {
    conversation {
        conversationId : adbabc54-3308-436d-a48b-932f4010d3c6,
        participantId : 415651e6-f0a5-4203-8019-4f88c3ed9cd5
    },
    person {
        firstname: "fred",
        surname: "smith",
        age: "21", 
        gender: "male"
    }
}

How would I do this dynamically whereby the result object is built using the name of the var 'conversation' and name of the object 'person' ?

Also, the length of either conversation or person can be any length. Pure JavaScript if possible , but could use underscore etc.

1
  • 1
    Have you tried simply doing var result = { conversation: conversation, person: person }; ? Commented Nov 11, 2016 at 11:05

4 Answers 4

3

Try it

var result = {
 'conversation': conversation,
 'person': person
}

Dynamic

var result = {}

result['person'] = person

or

resilt.person = person
Sign up to request clarification or add additional context in comments.

2 Comments

Check it alert( result.person )
Has to be dynamic can't be hardcoded the names, must be inferred from the vars.
2

If I understand your question correctly, you can use object shorthand notation which is supported in most browsers (Probably all of them, except IE11) for simplifying your solution even more:

var conversation = 
{
    conversationId : 'adbabc54-3308-436d-a48b-932f4010d3c6',
    participantId : '415651e6-f0a5-4203-8019-4f88c3ed9cd5'
};
var person = 
{
    firstname: "fred",
    surname: "smith",
    age: "21", 
    gender: "male"
};

var result = { conversation, person }

console.log(result)

EDIT: If only the variable name changes, and it's properties names stay the same or have some sort of unique key, you can use a for loop on the object's keys.

For example:

var someConversationVariableName = 
{
    conversationId : 'adbabc54-3308-436d-a48b-932f4010d3c6',
    participantId : '415651e6-f0a5-4203-8019-4f88c3ed9cd5'
};
var somePersonVariableName = 
{
    firstname: "fred",
    surname: "smith",
    age: "21", 
    gender: "male"
};

var result = { someConversationVariableName, somePersonVariableName }

for (key in result) {  
  if(result[key]['conversationId']) {
    console.log(`Found conversation object. It's name is: ${key}`);
  }
  else if(result[key]['firstname']) {
    console.log(`Found person object. It's name is: ${key}`);
  }
}
  

2 Comments

Yes, axlj and yourself have posted solutions that work, but the dynamic determination of the var names is the problem. What happens if 'conversation' changes and becomes 'talktome' ? As I mentioned to axly it's a dynamic piece of JS that I receive over the wire, at runtime I do not know the names of the vars. I'm going to have to parse the received JS code and work out (somehow) the var names. Alternatively, I get back to the originators of the JS sent to me and ask them to send a definition json object as well.
You can iterate on the object's keys(which is the variable names) using for loop.
1

If you need to defer adding objects, you can also take this approach:

var conversation = 
{
    conversationId : 'adbabc54-3308-436d-a48b-932f4010d3c6',
    participantId : '415651e6-f0a5-4203-8019-4f88c3ed9cd5'
};
var person = 
{
    firstname: "fred",
    surname: "smith",
    age: "21", 
    gender: "male"
};

var result = {};

result['conversation'] = conversation;
result['person'] = person;

console.log(result);

3 Comments

Almost :-) But I cannot have the names hardcoded. The Javascript that I get is itself dynamics (hard to explain) But I get a block of code sent with the var names already in it. I'm trying to build the result object from this block of code, hence the dynamic naming ...if this makes sense
Ah I hear you. What you're trying to achieve is not possible in the global scope. You'd need to define the object similar to I showed above and then loop through all of the keys of that object and place the child objects where you want them. let me know if this makes sense.
Yes, that's what I (we) thought
0

This Must work :-

var conversation =
{
    "conversationId": "adbabc54-3308-436d-a48b-932f4010d3c6",
    "participantId": "415651e6-f0a5-4203-8019-4f88c3ed9cd5"
}

var person=
{
    firstname: "fred", 
    surname: "smith", 
    age: "21", 
    gender: "male"
}

var result = {
 'conversation': conversation,
 'person': person
}

1 Comment

but it's not dynamic : var result = { 'conversation': conversation, 'person': person }

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.