2017-01-15 5 views
0

Я получил CSV-файл, который я умудряюсь convert to json, и я получил этуresctructuring JSON из CSV

[{ 
    first_name: 'account#1', 
    last_name: 'lastname#1', 
    email: '[email protected]', 
    category: 'category_a', 
    level_of_experience: '1-2 years', 
    type: 'media_type_a', 
    price: 'price_for_media_type_a', 
    work_sample: 'sample_for_media_type_a_1' 
}, { 
    first_name: 'account#1', 
    last_name: 'lastname#1', 
    email: '[email protected]', 
    category: 'category_a', 
    level_of_experience: '1-2 years', 
    type: 'media_type_a', 
    price: 'price_for_media_type_a', 
    work_sample: 'sample_for_media_type_a_2' 
}, { 
    first_name: 'account#1', 
    last_name: 'lastname#1', 
    email: '[email protected]', 
    category: 'category_a', 
    level_of_experience: '1-2 years', 
    type: 'media_type_b', 
    price: 'price_for_media_type_b', 
    work_sample: 'sample_for_media_type_b_1' 
}, { 
    first_name: 'account#1', 
    last_name: 'lastname#1', 
    email: '[email protected]', 
    category: 'category_a', 
    level_of_experience: '1-2 years', 
    type: 'media_type_b', 
    price: 'price_for_media_type_b', 
    work_sample: 'sample_for_media_type_b_2' 
}, { 
    first_name: 'account#1', 
    last_name: 'lastname#1', 
    email: '[email protected]', 
    category: 'category_b', 
    level_of_experience: '3-5 years', 
    type: 'media_type_c', 
    price: 'price_for_media_type_c', 
    work_sample: 'sample_for_media_type_c_1' 
}, { 
    first_name: 'account#1', 
    last_name: 'lastname#1', 
    email: '[email protected]', 
    category: 'category_b', 
    level_of_experience: '3-5 years', 
    type: 'media_type_c', 
    price: 'price_for_media_type_c', 
    work_sample: 'sample_for_media_type_c_1' 
}, { 
    first_name: 'account#1', 
    last_name: 'lastname#1', 
    email: '[email protected]', 
    category: 'category_b', 
    level_of_experience: '3-5 years', 
    type: 'media_type_d', 
    price: 'price_for_media_type_d', 
    work_sample: 'sample_for_media_type_d_1' 
}, { 
    first_name: 'account#1', 
    last_name: 'lastname#1', 
    email: '[email protected]', 
    category: 'category_b', 
    level_of_experience: '3-5 years', 
    type: 'media_type_d', 
    price: 'price_for_media_type_d', 
    work_sample: 'sample_for_media_type_d_2' 
}, { 
    first_name: 'account#2', 
    last_name: 'lastname#2', 
    email: '[email protected]', 
    category: 'category_c', 
    level_of_experience: '1-2 years', 
    type: 'media_type_ab', 
    price: 'price_for_media_type_ab', 
    work_sample: 'sample_for_media_type_ab_1' 
}, { 
    first_name: 'account#2', 
    last_name: 'lastname#2', 
    email: '[email protected]', 
    category: 'category_c', 
    level_of_experience: '1-2 years', 
    type: 'media_type_ab', 
    price: 'price_for_media_type_ab', 
    work_sample: 'sample_for_media_type_ab_2' 
}, { 
    first_name: 'account#2', 
    last_name: 'lastname#2', 
    email: '[email protected]', 
    category: 'category_c', 
    level_of_experience: '1-2 years', 
    type: 'media_type_cd', 
    price: 'price_for_media_type_cd', 
    work_sample: 'sample_for_media_type_cd_1' 
}, { 
    first_name: 'account#2', 
    last_name: 'lastname#2', 
    email: '[email protected]', 
    category: 'category_c', 
    level_of_experience: '1-2 years', 
    type: 'media_type_cd', 
    price: 'price_for_media_type_cd', 
    work_sample: 'sample_for_media_type_cd_2' 
}, { 
    first_name: 'account#2', 
    last_name: 'lastname#2', 
    email: '[email protected]', 
    category: 'category_d', 
    level_of_experience: '3-5 years', 
    type: 'media_type_ef', 
    price: 'price_for_media_type_ef', 
    work_sample: 'sample_for_media_type_ef_1' 
}, { 
    first_name: 'account#2', 
    last_name: 'lastname#2', 
    email: '[email protected]', 
    category: 'category_d', 
    level_of_experience: '3-5 years', 
    type: 'media_type_ef', 
    price: 'price_for_media_type_ef', 
    work_sample: 'sample_for_media_type_ef_1' 
}, { 
    first_name: 'account#2', 
    last_name: 'lastname#2', 
    email: '[email protected]', 
    category: 'category_d', 
    level_of_experience: '3-5 years', 
    type: 'media_type_gh', 
    price: 'price_for_media_type_gh', 
    work_sample: 'sample_for_media_type_gh_1' 
}, { 
    first_name: 'account#2', 
    last_name: 'lastname#2', 
    email: '[email protected]', 
    category: 'category_d', 
    level_of_experience: '3-5 years', 
    type: 'media_type_gh', 
    price: 'price_for_media_type_gh', 
    work_sample: 'sample_for_media_type_gh_2' 
}] 

Поступая сочетание lodash карты, GroupBy, и уменьшить, я смог решить до этого

[{ 
    "first_name": "account#1", 
    "last_name": "lastname#1", 
    "email": "[email protected]", 
    "details": { 
    "portfolio": { 
     "category_data": { 
     "category_a": { 
      "portfolio_type_data": { 
      "media_type_a": { 
       "cost": "price_for_media_type_a", 
       "sample_file": [ 
       null, 
       null, 
       null, 
       null, 
       null, 
       null, 
       null, 
       null 
       ] 
      }, 
      "media_type_b": { 
       "cost": "price_for_media_type_a", 
       "sample_file": [ 
       null, 
       null, 
       null, 
       null, 
       null, 
       null, 
       null, 
       null 
       ] 
      }, 
      "media_type_c": { 
       "cost": "price_for_media_type_a", 
       "sample_file": [ 
       null, 
       null, 
       null, 
       null, 
       null, 
       null, 
       null, 
       null 
       ] 
      }, 
      "media_type_d": { 
       "cost": "price_for_media_type_a", 
       "sample_file": [ 
       null, 
       null, 
       null, 
       null, 
       null, 
       null, 
       null, 
       null 
       ] 
      } 
      } 
     }, 
     "category_b": { 
      "portfolio_type_data": { 
      "media_type_a": { 
       "cost": "price_for_media_type_a", 
       "sample_file": [ 
       null, 
       null, 
       null, 
       null, 
       null, 
       null, 
       null, 
       null 
       ] 
      }, 
      "media_type_b": { 
       "cost": "price_for_media_type_a", 
       "sample_file": [ 
       null, 
       null, 
       null, 
       null, 
       null, 
       null, 
       null, 
       null 
       ] 
      }, 
      "media_type_c": { 
       "cost": "price_for_media_type_a", 
       "sample_file": [ 
       null, 
       null, 
       null, 
       null, 
       null, 
       null, 
       null, 
       null 
       ] 
      }, 
      "media_type_d": { 
       "cost": "price_for_media_type_a", 
       "sample_file": [ 
       null, 
       null, 
       null, 
       null, 
       null, 
       null, 
       null, 
       null 
       ] 
      } 
      } 
     } 
     } 
    } 
    } 
}, { 
    "first_name": "account#2", 
    "last_name": "lastname#2", 
    "email": "[email protected]", 
    "details": { 
    "portfolio": { 
     "category_data": { 
     "category_c": { 
      "portfolio_type_data": { 
      "media_type_ab": { 
       "cost": "price_for_media_type_ab", 
       "sample_file": [ 
       null, 
       null, 
       null, 
       null, 
       null, 
       null, 
       null, 
       null 
       ] 
      }, 
      "media_type_cd": { 
       "cost": "price_for_media_type_ab", 
       "sample_file": [ 
       null, 
       null, 
       null, 
       null, 
       null, 
       null, 
       null, 
       null 
       ] 
      }, 
      "media_type_ef": { 
       "cost": "price_for_media_type_ab", 
       "sample_file": [ 
       null, 
       null, 
       null, 
       null, 
       null, 
       null, 
       null, 
       null 
       ] 
      }, 
      "media_type_gh": { 
       "cost": "price_for_media_type_ab", 
       "sample_file": [ 
       null, 
       null, 
       null, 
       null, 
       null, 
       null, 
       null, 
       null 
       ] 
      } 
      } 
     }, 
     "category_d": { 
      "portfolio_type_data": { 
      "media_type_ab": { 
       "cost": "price_for_media_type_ab", 
       "sample_file": [ 
       null, 
       null, 
       null, 
       null, 
       null, 
       null, 
       null, 
       null 
       ] 
      }, 
      "media_type_cd": { 
       "cost": "price_for_media_type_ab", 
       "sample_file": [ 
       null, 
       null, 
       null, 
       null, 
       null, 
       null, 
       null, 
       null 
       ] 
      }, 
      "media_type_ef": { 
       "cost": "price_for_media_type_ab", 
       "sample_file": [ 
       null, 
       null, 
       null, 
       null, 
       null, 
       null, 
       null, 
       null 
       ] 
      }, 
      "media_type_gh": { 
       "cost": "price_for_media_type_ab", 
       "sample_file": [ 
       null, 
       null, 
       null, 
       null, 
       null, 
       null, 
       null, 
       null 
       ] 
      } 
      } 
     } 
     } 
    } 
    } 
}] 

в то время как я хочу, чтобы это было как этот

{ 
    "first_name":"account#1", 
    "last_name":"lastname#1", 
    "details":{ 
    "portfolio":{ 
     "category_data":{ 
      ... 
      "category_a":{   
       ... 
       "portfolio_type_data":{ 
       "media_type_a":{ 
        "price":"price_for_media_type_a", 
        "sample_file":["sample_for_media_type_a_1", "sample_for_media_type_a_2"] 
       }, 
       "media_type_b":{ 
        "price":"price_for_media_type_b", 
        "sample_file":["sample_for_media_type_b_1", "sample_for_media_type_b_2"] 
       } 
       } 
       ... 
      }, 
      "category_b":{   
       ... 
       "portfolio_type_data":{ 
       "media_type_c":{ 
        "price":"price_for_media_type_c", 
        "sample_file":["sample_for_media_type_c_1", "sample_for_media_type_c_2"] 
       }, 
       "media_type_d":{ 
        "price":"price_for_media_type_d", 
        "sample_file":["sample_for_media_type_d_1", "sample_for_media_type_d_2"] 
       } 
       } 
       ... 
      }, 
     } 
    } 
    } 
} 

А вот мой jsfiddle

+0

Holy moly man ... Чтобы быть любым уровнем эффективности, вы не хотите многократно перебирать все свои данные. – toastal

+0

@toastal, я знаю, я увлекся, я знаю, что в этом должен быть какой-то более короткий путь, но еще не понял, но – littlechad

+0

Вам это не понравится, но поскольку структура данных идет от квартиры к гнезду , это, вероятно, должно быть построено во всем в одном гигантском сокращении с пустым объектом '{}', аккумулятора. Циклируйте и обрабатывайте данные один раз и загружайте все в первый раз. – toastal

ответ

1

Я думаю, что имеет смысл рефакторинга и делать это на одной итерации (в основном, как предлагал @toastal). Вот как вы можете добиться этого с помощью магии lodash.

let reducedData = _.reduce(csvData, (memo, value) => { 
 
\t let userInfo = { 
 
\t \t first_name: value.first_name, 
 
\t \t last_name: value.last_name, 
 
\t \t email: value.email, 
 
\t \t level_of_experience: value.level_of_experience 
 
\t }; 
 

 
\t //Search for user info, and push into memo if not found 
 
\t let user = _.find(memo, userInfo); 
 

 
\t if (!user) { 
 
\t \t memo.push(userInfo); 
 
\t \t user = userInfo; 
 
\t } 
 

 
\t //Same trick with portfolio type. Lodash allows to check nested path with _.get call 
 
\t const typePropertyPath = `details.portfolio.category_data.${value.category}.portfolio_type_data.${value.type}`; 
 
\t let typeData = _.get(user, typePropertyPath); 
 

 
\t if (typeData) { 
 
\t \t typeData.sample_file.push(value.work_sample); 
 
\t } else { 
 
\t \t //Set if not found (lodash takes care of nesting) 
 
\t \t _.set(user, typePropertyPath, { 
 
\t \t \t price: value.price, 
 
\t \t \t sample_file: [value.work_sample] 
 
\t \t }); 
 
\t } 
 

 
\t return memo; 
 
}, []);

Надеется, что это помогает.

+0

Святая моля! Большое вам спасибо. – littlechad

+0

Добро пожаловать! :) –

Смежные вопросы