gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 02:08:37 26 4
gpt4 key购买 nike

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

$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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com