简体   繁体   中英

Create parent and child relationship using lodash

Combine both parent and chid JSON arrays based on the index value and add another found attribute to it.

Data: Comparison field: parentJSON - index childJSON - parent_index

Output: - parent - and its children - parent - and its children

parentJSON:
[{ index:1, name: 'a'}, {index:2, name: 'b'}, {index:3, name: 'c'}, {index:4, name: 'd'}]

childJSON:
[
  { index:1, name: 'aa', parent_index:1}, 
  {index:2, name: 'ab', parent_index:1}, 
  {index:3, name: 'ba', parent_index: 2}, 
  {index:4, name: 'bb', parent_index: 2}, 
  {index:5, name: 'ca', parent_index: 3}, 
  {index:6, name: 'ad', parent_index: 1}
]

output:
[
  { index:1, name: 'a'},
  { index:1, name: 'aa', parent_index:1, found: true}, 
  { index:2, name: 'ab', parent_index:1, found: true},
  { index:6, name: 'ad', parent_index:1, found: true},
  { index:2, name: 'b'},
  { index:3, name: 'ba', parent_index:2, found: true}, 
  { index:4, name: 'bb', parent_index:2, found: true},
  { index:3, name: 'c'},
  { index:5, name: 'ca', parent_index:3, found: true},
  { index:4, name: 'd'},
]

Plunker Link

Since you are using lodash , will keep my solution to be all in lodash.

The steps towards this problem will be:

  1. Is to map the childJSON , to check if the index of their parent exist or not

  2. Merge the both array of jsons into one.

  3. Sort them by name in ascending order.

Writing this in code will be :

 var childJSON = [ { index:1, name: 'aa', parent_index:1}, {index:2, name: 'ab', parent_index:1}, {index:3, name: 'ba', parent_index: 2}, {index:4, name: 'bb', parent_index: 2}, {index:5, name: 'ca', parent_index: 3}, {index:6, name: 'ad', parent_index: 1} ]; var parentJSON = [ { index:1, name: 'a'}, {index:2, name: 'b'}, {index:3, name: 'c'}, {index:4, name: 'd'} ]; childJSON = _.map(childJSON, function(child) { child.found = !!_.find(parentJSON, {index: child.parent_index}); return child; }); var newArray = _.concat(childJSON, parentJSON); newArray = _.sortBy(newArray, ['name']); console.log(newArray); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.js"></script> 

Map the child array to an object or Map using parent_index as keys

Then iterate parent array and check the above map for any children to include

 var childMap = childJSON.reduce((a,c)=> a.set(c.parent_index, (a.get(c.parent_index)|| []).concat(c)),new Map) var res = parentJSON.reduce((a,c)=>{ a.push(c) if(childMap.has(c.index)){ return a.concat(...childMap.get(c.index)) } return a; },[]) console.log(res) 
 .as-console-wrapper {max-height: 100%!important;} 
 <script> var parentJSON = [{ "index": 0, "parent_name": "Cline Walters" }, { "index": 1, "parent_name": "Tommie Hoover" }, { "index": 2, "parent_name": "Rosalie Foreman" }, { "index": 3, "parent_name": "Sutton Garza" }, { "index": 4, "parent_name": "Vega Estrada" }, { "index": 5, "parent_name": "Ballard Long" }, { "index": 6, "parent_name": "Ernestine Dalton" }]; var childJSON = [{ "index": 0, "child_name": "Keisha Simmons", "parent_index": 0 }, { "index": 1, "child_name": "Lane Walsh", "parent_index": 0 }, { "index": 2, "child_name": "Jocelyn Chapman", "parent_index": 1 }, { "index": 3, "child_name": "Weaver Welch", "parent_index": 1 }, { "index": 4, "child_name": "Short Jarvis", "parent_index": 2 }, { "index": 5, "child_name": "Dotson Washington", "parent_index": 3 }, { "index": 6, "child_name": "Pate Bradley", "parent_index": 4 }]; </script> 

You can combine them into one array, then sort by parent_index or index

 let par = [{ index:1, name: 'a'}, {index:2, name: 'b'}, {index:3, name: 'c'}, {index:4, name: 'd'}] let child = [{ index:1, name: 'aa', parent_index:1}, {index:2, name: 'ab', parent_index:1}, {index:3, name: 'ba', parent_index: 2}, {index:4, name: 'bb', parent_index: 2}, {index:5, name: 'ca', parent_index: 3}, {index:6, name: 'ad', parent_index: 1}] let res = [...par, ...child].sort((a, b) => (a.parent_index || a.index) - (b.parent_index || b.index) ); console.log(res); 

 let parentJSON = [{ index:1, name: 'a'}, {index:2, name: 'b'}, {index:3, name: 'c'}, {index:4, name: 'd'}] let childJSON = [ { index:1, name: 'aa', parent_index:1}, {index:2, name: 'ab', parent_index:1}, {index:3, name: 'ba', parent_index: 2}, {index:4, name: 'bb', parent_index: 2}, {index:5, name: 'ca', parent_index: 3}, {index:6, name: 'ad', parent_index: 1} ] let answer = []; parentJSON.forEach(parent => { answer.push(parent); childJSON.forEach(child => { if(child.parent_index === parent.index){ child.found = true; answer.push(child); } }) }); console.log(answer) 

Without knowing how you are expecting to have childJSON that is not exist in parentJSON , the above solution made an assumption that all elements of childJSON has a parent_index link to parentJSON .

Do let me know if you updated your question unless this is exactly what you want

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM