使用lodash创建父子关系

时间:2018-05-24 23:56:21

标签: javascript ecmascript-6 lodash

根据索引值合并parent和chid JSON数组,并为其添加另一个找到的属性。

数据:比较字段: parentJSON - 索引 childJSON - parent_index

输出: - 父母 - 和它的孩子 - 父母 - 和它的孩子

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

4 个答案:

答案 0 :(得分:1)

由于您使用的是lodash,因此我的解决方案将完全保留在lodash中。

解决这个问题的步骤是:

  1. 是否映射childJSON,以检查其父级索引是否存在

  2. 将两个jsons数组合并为一个。

  3. name按升序排序。

  4. 在代码中写这个:

    
    
    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>
    &#13;
    &#13;
    &#13;

答案 1 :(得分:0)

使用parent_index作为键将子数组映射到对象或Map

然后迭代父数组并检查上面的地图是否包含任何子项

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>

答案 2 :(得分:0)

您可以将它们组合成一个数组,然后按parent_index或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);

答案 3 :(得分:0)

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)

在不知道childJSON中不存在parentJSON的情况的情况下,上述解决方案假设childJSON的所有元素都有parent_index个链接到parentJSON

如果你更新了你的问题,请告诉我,除非这正是你想要的