Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(arg) {
- var luggage = {};
- var luggageSorted = {};
- var sortBy = arg[arg.length - 1];
- for (var i = 0; i < arg.length - 1; i++) {
- var row = arg[i].split(/\.*\*\.*/g);
- var name = row[0];
- var type = 'other';
- if (row[2] === 'true') {
- type = 'food';
- }
- if (row[3] === 'true') {
- type = 'drink';
- }
- if (!luggage.hasOwnProperty(name)) {
- luggage[name] = [];
- }
- luggage[name].push({
- 'clothe': row[1],
- 'kg': Number(row[5]),
- 'fragile': row[4] === 'true' ? true : false,
- 'type': type,
- 'transferredWith': row[6]
- });
- }
- var names = Object.keys(luggage);
- for (var i = 0; i < names.length; i++) {
- var name = names[i];
- sortLuggage(name);
- luggageSorted[name] = {};
- for (var j = 0; j < luggage[name].length; j++) {
- var clothe = luggage[name][j].clothe;
- luggageSorted[name][clothe] = luggage[name][j];
- delete luggageSorted[name][clothe].clothe;
- }
- }
- console.log(JSON.stringify(luggageSorted));
- function sortLuggage(name) {
- luggage[name].sort(function (a, b) {
- if (sortBy === 'luggage name') {
- return a.clothe > b.clothe ? 1 : -1;
- } else if (sortBy === 'weight') {
- return a.kg - b.kg;
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment