Arranging data based on various characteristics while maintaining parent/child connections

My data consists of an array of objects:

    x {sortOrder: 4, parentID: -1, questionID: 371}
    y {sortOrder: 2, parentID: -1, questionID: 800}
    z {sortOrder: 4, parentID: 23, questionID: 123}
    p {sortOrder: 1, parentID: -1, questionID: 371}
    q {sortOrder: 4, parentID: 371, questionID: 456}
    r {sortOrder: 3, parentID: -1, questionID: 371}
    s {sortOrder: 2, parentID: 800, questionID: 223}

I want to organize them in the following structure:

Sort Order

  • Parent ID

    • Child (Question ID)

The desired order based on the given example would be:

p y s r x q z

If needed, I am willing to change the "-1" representing a parent to a different value. My goal was to remove it from the sorting process by ensuring it cannot match an actual Question ID.

I have written code that successfully prioritizes the sort order and groups children together, but they are not grouped under their respective parents. All parents are grouped together instead.

    return (aSortorder < bSortorder ? -1 : (aSortorder > bSortorder) ? 1 : ((aParentid < bParentid) ? -1 : ((aParentid > bParentid) ? 1 : 0)));

I appreciate any assistance. Thank you.

Answer №1

Here is my approach to the problem:

OrganizeQuestionsByParent:function(x,y){
    var xElm = $(x).data();
    var yElm = $(y).data();     
    var xParent = xElm.bindParentid;
    var yParent = yElm.bindParentid;
    var xQuestionId = xElm.bindQuestionid;
    var yQuestionId = yElm.bindQuestionid;
    var xOrder = xElm.bindSortorder;
    var yOrder = yElm.bindSortorder;
    var xTempId;
    var yTempId;
    (xParent < 0) ? xTempId = xQuestionId + ".5" : xTempId = xParent + ".9";
    (yParent < 0) ? yTempId = yQuestionId + ".5" : yTempId = yParent + ".9";
    var outcome = xOrder - yOrder || xTempId - yTempId;
    console.log(outcome);        
    return outcome;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What steps are involved in extracting post data from the javascript DOM using php?

Having an issue where my JavaScript sends data to PHP, but the parsing in PHP keeps failing. The data is received by PHP and displayed in a text area, however, it needs proper formatting before being parsed. Can anyone advise on how to correctly format the ...

Combining Two DIVS Side by Side

Hey there, I am working on a timeline using two divs within a table cell. My goal is to have these divs overlap instead of appearing one below the other. The position attribute for all the DIVs inside the cell must remain unchanged due to the drag/drop fu ...

When the route is modified, the image fetched from the JSON disappears

When using a div tag to display JSON values on an image, I noticed that the values disappear when navigating to other pages and then returning to the home page. This issue is occurring as I develop with Angular.js NumberJson.get().then(function (resul ...

Sending JSON data back to React in a file format

I have a json file within the project that contains the necessary data to display in my component. Can someone guide me on how to properly access the json file in react and output the data from it? data.json { "dictionary": [ { "index": "1", ...

Utilize lodash/Javascript to organize the class into groups and keep track of the number

Looking to organize an array list containing classes and the users who attended. The goal is to generate an output that displays the users attending each class, grouping them by class and providing a count of total users. var arrList = [ { class: ' ...

Executing an AngularJS function through CasperJS is a common task that can

I am facing a challenge with testing a directive within a controller. The unit tests I am trying to write involve executing this code, which is triggered not by a click event but rather by a socket.io emit. Instead of attempting to mock socket.io, I am exp ...

Adding an Item to the Cart in React.js

Currently, I am delving into react.js and endeavoring to incorporate a fundamental add to cart feature. My aim is to maintain simplicity in order to grasp the entire process. The setup involves two cards - one showcasing the items and another where the sel ...

Interpolating within conditionals through pipes

Looking to make some modifications with interpolation: <div> {{ cond1 || cond2 || cond3}} </div> Any idea on how to apply a custom pipe like this: <div> {{ cond1 || cond2 || cond3 | customPipe }} </div> Attempted to use ...

Tips for using Angular's formatDate function to format dates

I am attempting to achieve a specific format using Angular's formatDate "2019-01-01T00:00:00Z" Here is the code I am using: formatDate( '2019-01-01', 'yyyy-MM-ddT00:00:00Z', 'en-US' ) The output I am getting is ...

Unable to link to 'mdMenuTriggerFor' as it is not a recognized attribute of 'button'

During the execution of my angular application using ng serve, I encounter the following error: Can't bind to 'mdMenuTriggerFor' since it isn't a known property of 'button' Despite importing all the necessary components, I a ...

Extracting Data from Input Box Using Query

In my HTML setup, I have five <textarea> tags with IDs text1,text2,text3,text4, and one additional <textarea> tag with ID output. I want to query based on the values of text1,text2,text3,text4 (referred to as field1, field2, field3, field4). F ...

Check through an array for any updates whenever a function is called and my react state is altered

I am currently working on a project related to playlists. My goal is to display the image attached to each song whenever that song is clicked from the playlist. Here is the code I have so far... const Component = () => { const value = useContext(DataC ...

What steps can be taken to ensure that JSON.parse() interprets all numbers as BigInt values?

I'm dealing with numbers in JSON that exceed the limits of the Number type, so I'd like to convert them to bigint. How can I do this? {"foo":[[0],[64],[89],[97]],"bar":[[2323866757078990912,144636906343245838,44169598393274215 ...

Android params not being transferred to Node.js properly when using Volley

I am currently developing an application that utilizes several node.js scripts for server scripting primarily due to node.js's built-in Firebase support. However, I have encountered an issue where I am unable to send any parameters with my requests, w ...

ng-class not displaying the correct results

Within my form, I have a condition where if $isvalidate is true, the button receives the class 'disable-reg-btn'. Here is an example: ng-class="{{reg_form.$invalid ? 'disable-reg-btn' : ''}}" However, when I view the page in ...

The interconnected promises of nodes and readability in coding

I've been experimenting with sending multiple request/responses from a node server, and due to their asynchronous nature, I decided to delve into learning about promises. My toolkit includes bluebird, node, and request for handling requests. My goal ...

Node.js error: Attempting to set property '' on an undefined object not allowed

I encountered an issue while attempting to update an array within a model. Even though the usagePlan is defined before the update, the error below keeps getting thrown. customer.usagePlan.toolUsage.tools.push(aNewToolObject); customer.updateAttribute(&apo ...

What is the best way to activate a function within an npm package in a Vue application?

I'm just starting out with Vuejs and I've recently installed the vue-countup-v2 npm package. I successfully imported it into my Vue component and noticed that it works perfectly when the page loads. However, I am interested in triggering the Coun ...

How can I use jcanvas to edit the drawImage layer in order to add cool Instagram-style filters to my image?

I recently created an image on canvas using the jcanvas drawImage method, making it a separate layer. //Here is the JavaScript code snippet: $('canvas').drawImage({ source: 'js/BN44094.jpg', layer: true, x: 0, y: 0, ...

How to set up Node 6.9 on Ubuntu Zesty 17.04

I am eager to upgrade NodeJS to version 6.9 on Ubuntu Zesty 17.04 but have been unsuccessful in doing so. Despite my attempts using apt-get or downloading from nodesource ppa, I consistently end up with version 4.7.2. Using Apt-get: sudo apt-get update s ...