2017-10-20 147 views
0

循環可能是錯誤的術語,但它描述了我正在嘗試的東西。 我想給平面數據結構,但我也需要跟蹤它來自的數組。拉姆達循環陣列

基本上我的規則(每個陣列):

  1. 如果1級exists-給它的項目的nametypechild陣列。每次出現1級(即使在相同的數組中),它應該創建一個新的條目。

  2. typechild內,把任何產品與水平> 1

  3. 如果NO水平1 exists-給它的項目的nametypechild陣列。

下面我的代碼是幾乎存在,所不同的是它應該創建它看到一個級別的陣列,每次1.我的例子纔有意義:

輸入數據

[ 
    { 
    "title": "Test 1", 
    "type": [{ 
     "name": "Animal", 
     "level": 1 
     }, 
     { 
     "name": "Food", 
     "level": 1 
     }, 
     { 
     "name": "Chicken", 
     "level": 3 
     } 
    ] 
    }, 
    { 
    "title": "Test 2", 
    "type": [{ 
     "name": "Foo", 
     "level": 2 
    }] 
    } 
] 

注:動物和食物都是LEVEL 1項。所以應該建立兩個數組是這樣的...

所需的輸出

[ 
    { 
     name: "Animal", 
     typechild: [ 
      { 
       level: 2, 
       name: "Chicken" 
      } 
     ] 
    }, 
    { 
     name: "Food", 
     typechild: [ 
      { 
       level: 2, 
       name: "Chicken" 
      } 
     ] 
    }, 
    { 
     name: "NoName", 
     typechild: [ 
      { 
       level: 2, 
       name: "Foo" 
      } 
     ] 
    } 
] 

Ramda嘗試(試一下:https://dpaste.de/JQHw):

const levelEq = (n) => pipe(prop('level'), equals(n)); 
const topLevel = pipe(prop('type'), find(levelEq(1))); 
const topLevelName = pipe(topLevel, propOr('NoName', 'name')); 
const extract2ndLevel = pipe(pluck('type'), flatten, filter(levelEq(2))); 

const convert = pipe(
    groupBy(topLevelName), 
    map(extract2ndLevel), 
    map(uniq), 
    toPairs, 
    map(zipObj(['name', 'typechild'])) 
); 
+0

爲什麼'Chicken' 2級? 'NoName'從哪裏來? – garajo

回答

1

像這樣的事情?

var output = [{ 
 
    "name": "Animal", 
 
    "typechild": [{ 
 
    "name": "Chicken", 
 
    "level": 3 
 
    }, { 
 
    "name": "Dog", 
 
    "level": 2 
 
    }] 
 
}, { 
 
    "name": "Food", 
 
    "typechild": [{ 
 
    "name": "Chicken", 
 
    "level": 3 
 
    }] 
 
}, { 
 
    "name": "No name", 
 
    "typechild": [{ 
 
    "name": "Foo", 
 
    "level": 2 
 
    }, { 
 
    "name": "Baz", 
 
    "level": 2 
 
    }] 
 
}] 
 

 

 
let out = {}, 
 
    typechild = {}, 
 
    k; 
 

 
const data = [{ 
 
    "title": "Test 1", 
 
    "type": [{ 
 
    "name": "Animal", 
 
    "level": 1 
 
    }, { 
 
    "name": "Food", 
 
    "level": 1 
 
    }, { 
 
    "name": "Chicken", 
 
    "level": 3 
 
    }] 
 
}, { 
 
    "title": "Test 2", 
 
    "type": [{ 
 
    "name": "Foo", 
 
    "level": 2 
 
    }] 
 
}, { 
 
    "title": "Test 3", 
 
    "type": [{ 
 
    "name": "Baz", 
 
    "level": 2 
 
    }] 
 
}, { 
 
    "title": "Test 4", 
 
    "type": [{ 
 
    "name": "Animal", 
 
    "level": 1 
 
    }, { 
 
    "name": "Dog", 
 
    "level": 2 
 
    }] 
 
}] 
 

 

 
data.forEach((node) => { 
 
    k = false; 
 
    typechild[node.title] = []; 
 
    node.type && node.type.forEach((t, i) => { 
 
    if (t.level == 1) { 
 
     k = true; 
 

 
     !out[t.name] ? out[t.name] = { 
 
     name: t.name, 
 
     typechild: typechild[node.title] 
 
     } : out[t.name].typechild = out[t.name].typechild.concat(typechild[node.title]); 
 
    } else { 
 
     typechild[node.title].push(t); 
 
    } 
 
    if (i == node.type.length - 1 && !k && typechild[node.title].length) { 
 
     out['No name'] = out['No name'] || { 
 
     name: 'No name', 
 
     typechild: [] 
 
     }; 
 

 
     out['No name'].typechild = out['No name'].typechild.concat(typechild[node.title]); 
 
    } 
 
    }); 
 
}); 
 

 
console.log(JSON.stringify(Object.values(out)));

+0

這並不完全如此 - typeChild:值顯示的是一個奇怪的「typechild」:/ ** ref:3 ** /'like values。你能確認typeChild部分是否正確? – Ycon

+0

在您的控制檯中測試。它在這裏抱怨同樣的參考。 – bluehipy

+0

謝謝,但一直未定義!在這裏試了一下 - https://jsfiddle.net/7pzqb2q2/ – Ycon