Javascript Array Sorting Headache -
I have articles that look like this:
[{id: { ID: 5, status: 2}, {id: 1, status: wrong}, {id: 2, status: wrong}, {id: 3, status: 4}, {id: 4, status: false} 6, Position: 5}, {id: 7, position: 3}, {id: 8, position: 1},] I would like to sort this array using the position property ( Ascending) of each object so that the array looks like this:
[id: 8, position: 1}, {id: 5, position: 2}, {id: 7, Status: 3}, {id: 3, status: 4}, {id: 6, status: 5}, {id: 1, status: wrong}, {id: 2, status: wrong}, {id: 4, status: false}] thoughts only The object is to move with a condition that is not false at the top, without affecting an object object without affecting the incorrectly set object.
Looks very easy, right? I have tried to do this problem in so many ways, but nothing is functioning steadily. Hopefully someone can help me with my head to go about this right.
Help me over the stack overflow, you are my only hope.
I tried to add a bunch of comments to help you follow your thoughts Has been implemented in a compulsory style, and (I hope) can help by explaining some ideas about the approach of similar problems.
I used your sample data set, and the result of which you were looking for again I hope it is useful and it can be modified to suit the data you need .
var deepSort = function (arr) {var i; Var hasPositionKey = []; Var noPositionKey = []; Var Nested Obj; var posts = []; Var logging administration = {}; Var positioned object = []; Repeat via the array of objects for // (i = 0; i & lt; arr.length; i ++); // is considered a nerated obz = ARR [i] for individual objects; // If there is a false state in that object (nested obz. Foundation === false) {// false object push in an array of objects with false status notation.e.pash (nested obje); } // If the object is a numbered one, then (type numberedObj.position === 'number') {// numbered position is the numbered position in the array of position objects. Peach (nested oboes); }} // condition (i = 0; i & lt; hasPositionKey.length; i ++) repeat through an array of objects {// nested obz = hopsi [i] is considered to be a for individual object Set the variable; // To track positions of posts, just push one position number into an array. Pat (nested obese.); // Add a significant value pair of position: ID for an object logging [nestedObj.position] = nestedObj.id; } // sorts the array which secures the conditions of non-false objects positions.sort (); // Iterate through that array and construct the last array of objects (i = 0; i & lt; positions.length; i ++) with the ordered objects: // Set a variable for the condition, As seen in the sequence: var num = positions [i]; Set a key for the object to create / var key = loggingPositions [num]; Set a value for the object to create / var value = num; // Object Create a result object with partial notation: var result = {ID: key, position: value}; // Pushing the building objects into position, the sliding object array position, the solved objects piss (the result); } // Returns a converged array of positions Returned Objects + Notional Objects Returns (Positioned Sourced Objects.Connect (NPO)); }; Var id = {{id: 1, status: wrong}, {id: 2, status: wrong}, {id: 3, status: 4}, {id: 4, status: wrong}, {id: 5, position : 2}, {id: 6, status: 5}, {id: 7, position: 3}, {id: 8, position: 1}]; DeepSort (sample);
Comments
Post a Comment