1

I have 2 different objects, with the same properties which i want to concatenate:

obj1 = {id: "1: identifier 1 \n", description: "1: description 1 \n", ...., propertyX1} obj2 = {id: "2: identifier 2 \n", description: "2: description 2 \n", ...., propertyX2} 

My result should be:

obj1 = {id: "1: identifier 1 \n 2: identifier 2 \n", description: "1: description 1 \n 2: description 2", ...., propertyX1|propertyX2} 

My solution so far is:

function combineElements(element1, element2){ var property1, property2; for (property1 in element1) { if(element1.hasOwnProperty(property1)) {//bonus question: why does intellij ask me to add this? property2 = element2.getSameProperty(property1); // <<<<<<how do I iterate this? property1 = "\n" + property2; } } return element1; } 

How would you get the exact same property of the second element?

3
  • 1
    element2[property1] Commented Jul 21, 2018 at 9:01
  • whart do you want with propertyX1 and propertyX2? Commented Jul 21, 2018 at 9:07
  • used ..., propertyX1 to denote that you don't really know hoe long the list of properties is. You just want to concat all of them. Commented Jul 21, 2018 at 9:14

4 Answers 4

3

You could concat all properties with the same name.

Techniques use (in order of apperance):

var obj1 = { id: "1: identifier 1 \n", description: "1: description 1 \n", propertyX1: 'propertyX1' }, obj2 = { id: "2: identifier 2 \n", description: "2: description 2 \n", propertyX2: 'propertyX2' }, result = [obj1, obj2].reduce( (r, o) => Object.assign(r, ...Object.entries(o).map(([k, v]) => ({ [k]: (r[k] || '') + v }))), {} ); console.log(result);

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

3 Comments

I don't think making dense one liners is the way to go with someone not knowing how to access a property.
@destoryer, right, that might be the problem, but i try to write the best possible answer. it might have a higher degree of knowing what part is doing, but op is free to ask, and anybody her can answer or edit this question.
Works! But as above comment says i dont really understand how. I would like to learn how to do the above :) any tips on keywords to google for to start to learning this?
2
function combineElements(element1, element2) { Object.getOwnPropertyNames(element1).forEach(function(k1) { if (element2[k1]) { element1[k1] = element1[k1] + " " + element2[k1]; } }); } 

find the matching keys for the 2 objects

Comments

0

Just traverse one of the objects and make a new one like that.

obj1 = {id: "1: identifier 1 \n", description: "1: description 1 \n"} obj2 = {id: "2: identifier 2 \n", description: "2: description 2 \n"} conObj = {}; for (var prop in obj1){ if (obj1.hasOwnProperty(prop)) { conObj[prop] = obj1[prop] + obj2[prop]; } } console.log(conObj);

Comments

0

You can use Set to hold the individual keys and iterate using Set.forEach and Array.join to concatenate the values.

var obj1 = {id: "1: identifier 1 \n", description: "1: description 1 \n", propertyX1:"propertyX1", propertyX3:"propertyX3"}; var obj2 = {id: "2: identifier 2 \n", description: "2: description 2 \n", propertyX2:"propertyX2", propertyX4:"propertyX4"}; var keys = new Set(Object.keys(obj1).concat(Object.keys(obj2))); var result={}; keys.forEach(function(key){ result[key] = [obj1[key], obj2[key]].join(" "); }); console.log(result)

1 Comment

Wouldn't that be Set.forEach?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.