Sorting a parent array in AngularJS using a child array's Date field as the basis

I have an array of arrays of objects with the following structure:

parentArray = [
[{id:1, date:1505020200000}, {id:4, date:1505020200000 }],
[{id:2, date:1504681500000}],
[{id:3, date:1504671000000}, {id:20, date:1504671000000}]
]

Each nested array contains dates that are all the same.

I am attempting to utilize Angular's orderBy filter to sort the dates from oldest to newest, but I am uncertain about the process.

I attempted setting the dateFilter variable for orderBy filter like this:

$scope.dateFilter= arr[0][0].date 
<div ng-repeat="child in parentArray | orderBy: dateFilter">

however, it seems that something is not right.

EDIT:

Many thanks to Matthew for his assistance and guidance. With his help, I realized the solution. Utilizing array.sort was what worked best for my data structure.

$scope.results.sort(function (a, b) {
  return a[0].date - b[0].date;
});

Answer β„–1

To simplify an array, create a personalized filter:

(function(app){
    app.filter("simplifyArray", function() {
        return function(input) {
            var output = [];
            input.forEach(function(innerArr) {
                output = output.concat(
                    innerArr.map(function(obj) { return obj; })
                );
                return output;
            });
            return output;
        };
    });
}(angular.module("yourModule")));

Update your ng-repeat to look like this:

<div ng-repeat="child in parentArr | simplifyArray | orderBy: 'date'">

Check out the CodePen Demo here

Answer β„–2

To facilitate sorting, create a function to retrieve the value for sorting.

$scope.myDateGetter = function(item) {
    return item[0].date;
}

Next, use this function as the expression in the orderBy filter.

<div ng-repeat="child in parentArr | orderBy: myDateGetter">

Further information can be found at: https://docs.angularjs.org/api/ng/filter/orderBy

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

In TypeScript, the function is failing to retrieve the complete array value

I'm facing an issue with a program that is supposed to piece together all the letters, but it's not functioning correctly. I've tried using the debugger, but it doesn't display any errors. Here's the code snippet: var phrase = [ &a ...

TextGeometry failing to render

Currently experimenting with TextGeometry. Successfully implemented BoxGeometry, but encountering issues with TextGeometry. Experimenting with different material options like MeshNormalMeterial, however, still unable to resolve the issue var scene = new ...

The second attempt at an AJAX call is unsuccessful

I am currently developing a form that displays database results based on two entries: Automarke (brand) and Modell (model). You can view the entries here. The Modell dropdown dynamically changes based on the selected Automarke. Here is the code snippet I ...

Ways to alter the color of an icon when hovering over it

Is there a way to change the color of a material icon inside an IconButton material component when hovering over the IconButton? I've tried adding a class directly to the icon, but it only works when hovering over the icon itself and not the IconButto ...

The term 'undefined' does not refer to an object

My div contains the following code: <em id="ProductPrice" class="ProductPrice VariationProductPrice">$75.00</em> I want to change the text color if the value changes. This is what I tried: <script> $(document).ajaxSuccess(function(){ ...

Utilize the id in AngularJS to bring attention to a specific row within a table

I am brand new to using angularjs. I currently have a table structured like this: HTML <table class="table table-striped" id="manageResumeTable"> <thead class="text-center text-info text-capitalize"> <th class="text-center col- ...

Unleashing the Power of Json Strings

After searching for 7 years, I have come across this old unanswered question: getting value from json string php. I am in need of assistance as my code is in the following format: Upon using json_decode(), the var_dump output was: String(384) β€œ{β€œdat ...

Transitioning between javascript functions

Having some issues with a switch case statement, also tried using if-else but no luck. In the HTML code: <select onBlur="functionCalc()" id="art"> <option value="hours" id="hours">Hours</option> <option value="minutes" id="mins">M ...

Transmit the identifier of the span tag to the URL seamlessly without the need to refresh

<div id="unique50">Greetings</div> Transfer the code 50 to the designated link by clicking on the specific div element without requiring a page reload ...

Vue.js: SCSS @import being overlooked

I've found great success using VueJS in two different projects. As I prepare to launch these projects, I'm encountering an issue when generating the files with npm run build. One project, created recently, is working fine. However, the other pro ...

Tips on changing the name of a property within an object using JavaScript

While this question may appear to be a duplicate, there is actually a distinction. I am attempting to provide a new key that does not contain any spaces. {order_id :"123" , order_name : "bags" , pkg_no : "00123#"} My goal is ...

Validation script needed for data list selection

<form action="order.php" method="post" name="myForm" id="dropdown" onsubmit="return(validate());"> <input list="From" name="From" autocomplete="off" type="text" placeholder="Starting Point"> <datalist id="From"> <option ...

Ways to display an SVG spinner prior to a substantial UI refresh

I am currently facing an issue with updating 10 apexcharts bar charts simultaneously in a Vue app. When this process occurs, it takes approximately one second to load completely, and during that time, I would like to display an svg spinner. However, the co ...

Modify opacity of displayed items in image list with ng-repeat

My application showcases a list of images using ng-repeat. +---------------------------------------------+ | | Previous Image 0 | | | +------------------------------------+ | | +------------------------------------+ | | ...

Create a generic function that retrieves a specific property from an array of objects using the select method

Currently, I have implemented four functions that select entries from an array based on different properties. if ($scope.filters.filter1) $scope.filteredEntries = $scope.filteredEntries.filter(function (o) { return o.field1 === $scope.filt ...

What could be causing the Twitter Timeline to fail to load based on a variable in a Vue.js component?

My goal is to display the Twitter timeline of multiple accounts based on the route. I initially attempted to use a plugin called vue-tweet-embed, but encountered issues with it. As a result, I resorted to the traditional method by embedding twitter's ...

Code displayed on Facebook when sharing a website link implemented in JavaScript

Is there a way to prevent javascript code from appearing in Facebook posts when sharing links, whether it's done manually or through programming? I'm specifically looking for solutions to fix my website so that when I post links on Facebook, the ...

Continue looping in Javascript until an empty array is identified

Currently, I am in search of a solution to create a loop in Javascript that continues until the array of objects is empty. The object I am working with looks like this: "chain": { "evolves_to": [{ "evolves_to": [{ ...

Switch the dropdown menu to a button if it consists of only one option

I have been using a database within the Moodle Learning Management System that generates a bootstrap table. Here is an example of what it looks like: The last row in the table contains a dropdown menu. When viewing your own entry, you have the options to ...

Difficulty establishing a connection between data in app.js and index.html using Ionic framework

Struggling with connecting index.html to get data for my Ionic mobile app. Any tips or guidance would be highly appreciated as I'm new to this! The goal is to display a list of restaurants in the app, utilizing Cards Images to showcase an image, logo ...