web建站教程
  1. 首页
  2. vuejs
  3. js
  4. 好玩
  5. AIGC工具
  6. 前端知识
  7. 百度echarts
  8. 更多
    php入门
    nodejs
    mockjs
    reactjs
    mysql
    wordpress
    织梦cms
    帝国cms
    git教程
    IT知识
    模板大全
    休息站
    手机应用

js如何让一个数组根据另一个数组的顺序进行排序

1301 ℃

功能介绍:一个数组显示基础数据,一个数组显示排序顺序,让基础数据数组根据排序数组的顺序排序!

具体代码如下:

const data = [
  { id: 1, name: "web3" },
  { id: 2, name: "html5" },
  { id: 3, name: "css3" },
  { id: 4, name: "nodejs" },
  { id: 5, name: "vuejs" },
  { id: 6, name: "php" }
];
const order = [2, 4, 1, 6, 5, 3]; //根据这个顺序对id进行排序。也可以改成name

//方法
hash = {}
data.forEach(function(item) { hash[item.id] = item }) //id:根据需要修改成自己的参数
results = order.map(function(item) { return hash[item] })

//输出结果
console.log('输出结果:', results)
//输出结果:
//(6) [{…}, {…}, {…}, {…}, {…}, {…}]
//0: {id: 2, name: 'html5'}
//1: {id: 4, name: 'nodejs'}
//2: {id: 1, name: 'web3'}
//3: {id: 6, name: 'php'}
//4: {id: 5, name: 'vuejs'}
//5: {id: 3, name: 'css3'}
//length: 6

php如何合并数组并去除重复值(重新排序)

js如何利用sort对各种数组元素进行排序

js如何实现数组元素倒序

js如何对数组进行排序

新手入门:掌握JS入门的几大小技巧

标签: 数组排序

上面是“js如何让一个数组根据另一个数组的顺序进行排序”的全面内容,想了解更多关于 js 内容,请继续关注web建站教程。

当前网址:https://ipkd.cn/webs_3458.html

声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

当前位置: 网站首页 > js
本文共计575个字,预计阅读时长4分钟

基金从业资格考试题库

一站式备考基金从业资格考试,收录2021-2025年模拟题库!呱呱工具箱

AI工作站

收录全球3800+ 款各行各业AI应用,轻轻松松做事!
生活小工具,收录了80多款小工具
上一篇: 推荐一个可以查询老黄历的网站,可以根据五行取名!
下一篇: 详细介绍HTTP状态码不同之处的原因(什么原因导致的)
x 打工人ai神器