Skip to content

Instantly share code, notes, and snippets.

@shklqm
Created October 30, 2018 20:17
Show Gist options
  • Save shklqm/b3396748874aa4d864be97c919ff7d91 to your computer and use it in GitHub Desktop.
Save shklqm/b3396748874aa4d864be97c919ff7d91 to your computer and use it in GitHub Desktop.
import moment from "moment-timezone";
export const mapObjectsToArray = (objects) => {
let index = -1, result = Array(objects.size);
Object.keys(objects).forEach((objectId)=>{
result[++index] = [objects[objectId]["createdAt"], objects[objectId]];
});
return result;
};
export const unmapObjectsFromArray = (objectsArrays) => {
let result = [];
objectsArrays.forEach((_objectArray)=>{
result.push(_objectArray[1]);
});
return result;
};
export const reorderObjects = (targetObjects) => {
let parsedObjects = {};
let result = [];
if (targetObjects){
Object.keys(targetObjects).forEach((targetObjectId) => {
let parentId = targetObjects[targetObjectId]["parent"];
if(parentId == null){
if(!(targetObjectId in parsedObjects)){
parsedObjects[targetObjectId] = targetObjects[targetObjectId];
parsedObjects[targetObjectId]["replies"] = [];
}
}
else{
if(!(parentId in parsedObjects)){
parsedObjects[parentId] = targetObjects[parentId];
parsedObjects[parentId]["replies"] = [];
}
parsedObjects[parentId]["replies"].push(targetObjects[targetObjectId]);
}
});
//sorting inner targetObjects
Object.keys(parsedObjects).forEach((targetObjectId) => {
if(parsedObjects[targetObjectId]["replies"].length){
parsedObjects[targetObjectId]["replies"] = unmapObjectsFromArray(
mapObjectsToArray(parsedObjects[targetObjectId]["replies"]).sort(
(a, b) => moment(a[1].createdAt) - moment(b[1].createdAt)
)
);
}
});
//sorting outer targetObjects
Object.keys(parsedObjects).forEach((targetObjectId) => {
result = unmapObjectsFromArray(mapObjectsToArray(parsedObjects)
.sort((a, b) => moment(a[1].createdAt) - moment(b[1].createdAt)));
});
}
return result;
};
@shklqm
Copy link
Author

shklqm commented Oct 30, 2018

Input example

{  
  "85384":{  
    "id":85384,
    "content":"Lores Ipsum",
    "parent":null,
    "createdAt":"2018-10-23T09:27:13.161522Z"
  },
  "85426":{  
    "id":85426,
    "content":"Lores Ipsum",
    "parent":null,
    "createdAt":"2018-10-23T09:27:17.128810Z"
  },
  "85400":{  
    "id":85400,
    "content":"Lores Ipsum",
    "parent":null,
    "createdAt":"2018-10-23T09:27:17.041750Z"
  },
  "85385":{  
    "id":85385,
    "content":"Lores Ipsum",
    "parent":null,
    "createdAt":"2018-10-23T09:27:16.995098Z"
  },
  "85415":{  
    "id":85415,
    "content":"Lores Ipsum",
    "parent":null,
    "createdAt":"2018-10-23T09:27:17.096840Z"
  },
  "85570":{  
    "id":85570,
    "content":"Lores Ipsum",
    "parent":85384,
    "createdAt":"2018-10-26T14:41:43.940199Z"
  },
  "85569":{  
    "id":85569,
    "content":"Lores Ipsum",
    "parent":85384,
    "createdAt":"2018-10-26T14:18:57.130353Z"
  },
}

@shklqm
Copy link
Author

shklqm commented Oct 30, 2018

Output example

[
  {
    "id":85384,
    "content":"Lores Ipsum",
    "parent":null,
    "createdAt":"2018-10-23T09:27:13.161522Z",
    "replies":[
      {
        "id":85569,
        "content":"Lores Ipsum",
        "parent":85384,
        "createdAt":"2018-10-26T14:18:57.130353Z"
      },
      {
        "id":85570,
        "content":"Lores Ipsum",
        "parent":85384,
        "createdAt":"2018-10-26T14:41:43.940199Z"
      }
    ]
  },
  {
    "id":85385,
    "content":"Lores Ipsum",
    "parent":null,
    "createdAt":"2018-10-23T09:27:16.995098Z",
    "replies":[

    ]
  },
  {
    "id":85400,
    "content":"Lores Ipsum",
    "parent":null,
    "createdAt":"2018-10-23T09:27:17.041750Z",
    "replies":[

    ]
  },
  {
    "id":85415,
    "content":"Lores Ipsum",
    "parent":null,
    "createdAt":"2018-10-23T09:27:17.096840Z",
    "replies":[

    ]
  },
  {
    "id":85426,
    "content":"Lores Ipsum",
    "parent":null,
    "createdAt":"2018-10-23T09:27:17.128810Z",
    "replies":[

    ]
  }
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment