score:3
Accepted answer
You can sort()
the keys by their associated count value and slice()
the result:
let data = {
"entity": {
"element1": 1,
"element2": 2,
"element3": 3,
"element4": 4,
"element5": 5,
"element6": 6,
"element7": 7
}
},
mymap = data.entity;
let elements = Object.keys(mymap)
.sort((a, b) => mymap[b] - mymap[a])
.slice(0, 5);
let counts = elements.map(key => mymap[key]);
console.log('5 highest counts', JSON.stringify(counts))
console.log('5 highest elements', JSON.stringify(elements))
score:0
You could use the angularJS $filter service to do the sorting and top n:
(function() {
var app = angular.module("soDemo", []);
app.controller("soDemoController", SoDemoController);
SoDemoController.$inject = ['$scope', '$filter'];
function SoDemoController($scope, $filter) {
var vm = {
counts: [{
name: 'element1',
count: 15
},
{
name: 'element2',
count: 26
},
{
name: 'element3',
count: 37
},
{
name: 'element4',
count: 45
}
]
};
var limitToFilter = $filter('limitTo');
vm.sortedCounts = limitToFilter($filter('orderBy')(vm.counts, '-count'), 3);
$scope.vm = vm;
return;
}
})();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div class="sample" ng-app="soDemo" ng-controller="soDemoController">
<h2>Counts</h2>
<pre>{{vm.counts | json}}</pre>
<h2>Sorted Counts (top 3)</h2>
<pre>{{vm.sortedCounts | json}}</pre>
</div>
score:0
Object.keys an map.
const entity = {
"element1": 99,
"element2": 15,
"element3": 26,
"element4": 37,
"element5": 45,
"element6": 2
};
const result = Object.keys(entity).map(key => ({ key, value: entity[key] })).sort((elemA, elemB) => {
if (elemA.value > elemB.value) {
return -1;
} else if (elemB.value > elemA.value) {
return 1;
}
return 0;
}).slice(0, 5);
//
// [ { key: 'element1', value: 99 },
// { key: 'element5', value: 45 },
// { key: 'element4', value: 37 },
// { key: 'element3', value: 26 },
// { key: 'element2', value: 15 } ]
console.log(result);
Just add another map or reduce in the end get the result in desired format.
Source: stackoverflow.com
Related Query
- how to sort and select top 5 elements in a json array using angular JS
- how to map serial and data points using json Array
- How to set json data from url on highchart and polling the json data after 1 sec to dislay on highchart using Angular js
- how to display drilldown column highchart using json array objects - 3 level drilldown column highchart
- How to create a column range chart in Highcharts using range and navigator functions?
- Highcharts: How to load data from JSON to xAxis.categories and series.data?
- How to Check and Uncheck all the Legend elements in HighCharts Linechart?
- How to structure Angular with Highcharts and lots of dynamic data
- How get data name in Highcharts pie chart legend instead of "Slice" using array of values?
- How to insert json within an array into highcharts?
- Angular Highcharts - How to enable noData options and update it dynamically
- How to enable noData with highcharts and angular
- How to draw a bubble map with longitude and latitude using Highcharts in react
- How to remove the value and number labels from Highcharts angular gauge
- How to set 'lang' property in Highcharts using Angular JS?
- How to Populate Highchart using Json Object
- Creating Highcharts with Angular using Highcharts >= 5.0.0 and highcharts-ng >= 1.0.0 using a ChartFactory
- Place Data in Highchart using json and post url
- Highcharts smooth transition on data update using Angular and ng-highcharts
- How to read and insert correct highchart number in selectors using Selenium Webdriver with Python?
- How can I hide non-selected series and axes when using XY zoom with highcharts?
- How would I convert this array to this json format
- How can I automate high charts and graphs using selenium webdriver?
- How to draw multiple highcharts using highcharts-ng and angularjs
- How could you supply multiple series to a line chart in R shiny using highcharter package and without hardcoding the series?
- Angular 11 - How to import array data into highcharts
- Angular JSON from API to an Array of value
- How to do grouping in Array of Objects in angular 8
- finding the value in the series object and displaying in the tooltip of the high chart using angular 4
- how to show one more array in the tooltip using highchart?
More Query from same tag
- Show average in highcharts graph as column
- How Do I put Multiple Charts on the Same Graph rCharts High Charts
- Drilldown in Map with Vue.js
- How can I sort the stacked bar charts in Highcharts by the sum of positive response?
- Example to show/hide data with buttons/checkbox in Highcharts?
- HighCharts onClick get column data on Stacked Column Chart
- Tickinterval of week,month,year in highcharts area spline chart
- Delay after addpoint when y-axis with opposite: true
- how to place the chart in the centre while using django-chartit?
- Manual series names in highcharter
- How to remove border radius when use Highcharts renderer.rect function
- Highcharts add points but not to all categories
- How to use custom JSON (not GeoJSON) with Highcharts map?
- Revert line style in highcharts v4 with null values to old style (v2.2)
- highcharts: individual serie shift
- Adding different y-axis name to all different chart in synchronous chart Highchart
- HighChart Libraries Android: HIOptions not found in HIChartView
- Where can I find the API documentation for older versions of Highcharts?
- Enabling markers for hover doesn't appear to work
- Highcharts: Is it possible to display series with the same name in different columns?
- Display Image on xAxis in highchart
- Update color of plotline label on change highcharts
- How can I make milestone lines with a GANTT chart using the highcharts library?
- HighCharts Drill down not working
- Hide Data Labels in Pie Chart below 400px width - Highcharts
- Tags disappeared from tagcloud in highcharts while using deriveFontSize method
- How to tell Highcharts to set date on the x axis, and use a range of time (like 1 day)
- Yii - Highcharts - Cannot get chart to show
- Highstock graph shows points instead of line at certain zoom levels
- Highstock - change tickpositioner dynamically