1

I have two arrays:

 a = [{"sourceId": "1", "targetId": "2", "name": "heats air"} , 
      {"sourceId": "3", "targetId": "4", "name": "power"}]

 b = [{"name": "Hair Dryer", "id": "1"}, 
      {"name": "Heating System", "id": "2"}, 
      {"name": "Mains", "id": "3"}, 
      {"name": "Blower", "id": "4"}]

How do I get the output like this:

[{"sourceId": "1", "targetId": "2", "name": "heats air", "from": "Hair Dryer", "to": "Heating System"}, 
 {"sourceId": "3", "targetId": "4", "name": "power","from": "Mains", "to": "Blower"]

I want to merge them based on the property values: the keys "sourceId" and "targetId" of array a should correspond to the key "id" of array b. If a sourceId is matched with an id, add the value of the name with key "from" to the object in array a; If a targetId is matched with an id, add the value of the name with key "to" to the item in array a. Also,I am wondering whether I can do this without using lodash. (using ES6)

3
  • 1
    It is unclear what the merging algorithm is supposed to be as the objects that get merged have no common properties. How do you determine what gets merged with what? Please describe "in words" what the merging algorithm is. And, how do you determine what from and to should be set to? Commented Sep 1, 2017 at 15:46
  • The keys "sourceId" and "targetId" of array a should correspond to the key "id" of array b. Commented Sep 1, 2017 at 15:49
  • Please edit your question to add the merging algorithm to your question. Also describe how from and to are supposed to be set. Comments should not be used for required information - that information should be added to the question itself. Commented Sep 1, 2017 at 15:52

2 Answers 2

1

Convert b to a Map of id to name using Array#reduce. Then Array#map a to the required form using Object#assign, and the bMap:

const a = [{"sourceId":"1","targetId":"2","name":"heats air"},{"sourceId":"3","targetId":"4","name":"power"}];

const b = [{"name":"Hair Dryer","id":"1"},{"name":"Heating System","id":"2"},{"name":"Mains","id":"3"},{"name":"Blower","id":"4"}];

const bMap = b.reduce((map, item) => map.set(item.id, item.name), new Map);

const result = a.map((item) => (Object.assign({
  from: bMap.get(item.sourceId),
  to: bMap.get(item.targetId)
}, item)));

console.log(result);

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

Comments

0

Here you go.

 const a = [{"sourceId": "1", "targetId": "2", "name": "heats air"} , 
      {"sourceId": "3", "targetId": "4", "name": "power"}]

 const b = [{"name": "Hair Dryer", "id": "1"}, 
      {"name": "Heating System", "id": "2"}, 
      {"name": "Mains", "id": "3"}, 
      {"name": "Blower", "id": "4"}]
      
 const result = a.reduce((arr, curr) => {
 	const from = b.filter(bObj => {
  	return bObj.id === curr.sourceId;
  })[0]
  const to = b.filter(bObj => {
  	return bObj.id === curr.targetId;
  })[0];
  arr.push({ ...curr, from: from.name, to: to.name });
  return arr;
 }, []);
 
 console.log(result);

Comments

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.