Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // var tree = [
- // {
- // "children": [
- // {
- // "id": "BioFine",
- // "label": "BioFine"
- // },
- // {
- // "id": "Brewing Salts",
- // "label": "Brewing Salts"
- // },
- // {
- // "id": "CO2",
- // "label": "CO2"
- // },
- // {
- // "id": "Hops",
- // "label": "Hops"
- // },
- // {
- // "id": "Kettle Coagulants",
- // "label": "Kettle Coagulants"
- // },
- // {
- // "id": "Malt",
- // "label": "Malt"
- // },
- // {
- // "id": "O2",
- // "label": "O2"
- // },
- // {
- // "id": "Yeast",
- // "label": "Yeast"
- // }
- // ],
- // "id": "Beer",
- // "label": "Beer"
- // },
- // {
- // id: 'fruit',
- // label: 'Fruit',
- // children: [
- // { id: 'apple', label: 'Apple' },
- // { id: 'banana', label: 'Banana',
- // children: [
- // { id: 'cherry', label: 'Cherry' }
- // ] }
- // ]
- // }
- // ];
- var flattenList = [];
- var flattenObject = function(tree) {
- _.forEach(tree, function(value, i) {
- if (!_.has(tree, i)) return;
- if (_.isObject(tree[i]) && !_.isNull(tree[i])) {
- var flatObject = flattenObject(tree[i]);
- _.forEach(flatObject, function(v, x) {if (!_.has(flatObject, x)) return;});
- } else {
- if(i === "label"){
- flattenList.push(tree[i]);
- }
- }
- });
- return flattenList;
- };
Add Comment
Please, Sign In to add comment