javascript - 使用数组对对象进行排序并将值存储在另一个数组中

标签 javascript arrays angularjs sorting

我有一个对象参数,其属性和值类似于

$scope.chartOptions.response.items.count = { amazing: 8, amenities: 6, awesome: 7, beautiful: 15, better: 9, clean: 12, comfortable: 8, complaint: 6, definitely stay: 6, excellent: 11, friendly: 17, front desk: 8, good: 15, gorgeous: 6, great: 28, helpful: 11, nice: 15, perfect: 7, the best: 10, wonderful: 12 }

该数组有 20 个索引值。该数组看起来像

series[] = ["great", "friendly", "good", "beautiful", "nice", "wonderful", "clean", "excellent", "helpful", "the best", "better", "comfortable", "front desk", "amazing", "perfect", "awesome", "amenities", "complaint", "gorgeous", "definitely stay"]

我想根据数组中的值对对象进行排序,并将所有数值存储在第二个数组中,例如series2[]= [28, 17, 15...] 使用 angularjs 和 javascript 是否可以实现这一点

最佳答案

使用Array.map()迭代系列 ,并通过当前字符串从 count 中获取值:

const count = { amazing: 8, amenities: 6, awesome: 7, beautiful: 15, better: 9, clean: 12, comfortable: 8, complaint: 6, 'definitely stay': 6, excellent: 11, friendly: 17, 'front desk': 8, good: 15, gorgeous: 6, great: 28, helpful: 11, nice: 15, perfect: 7, 'the best': 10, wonderful: 12 };
const series = ["great", "friendly", "good", "beautiful", "nice", "wonderful", "clean", "excellent", "helpful", "the best", "better", "comfortable", "front desk", "amazing", "perfect", "awesome", "amenities", "complaint", "gorgeous", "definitely stay"];


const sortedNumbers = series.map((str) => count[str]);

console.log(sortedNumbers);

关于javascript - 使用数组对对象进行排序并将值存储在另一个数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49680842/

相关文章:

javascript - 如何删除 Bootstrap 下拉菜单的 preventDefault 行为

javascript - 沿对 Angular 线拖放?

javascript - 通过属性 javascript 连接对象数组

c - 关于复制垃圾

Angular Controller 上的 JavaScript promise

json - Angular 资源将一维字符串数组解析为 2d

javascript - 如何在 $http.post() 之后从 Angular 导航到 php 页面

javascript - SVG的宽度:g using getBBox() returns wrong number

php - 在 PHP 中获取数组的最终键

javascript - gulp,任务单独工作,但不能分组工作