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

js用最简单的方法实现2个数组的交叉一对一合并

381 ℃
           
let data1 = [{"name": '小米'}, {"name": '小红'}, {"name": '小花'}, {"name": '小明'}];
let data2 = [
  {"school": "清北", "type": "本科"}, 
  {"school": "浙大", "type": "研究生"}, 
  {"school": "复旦", "type": "大专"}, 
  {"school": "剑桥", "type": "硕士"}
];

let result = data2.map(z=>data1.map(x=>({...x, ...z}))).flat()
console.log('输出结果:',result);
[
    {
        "name": "小米",
        "school": "清北",
        "type": "本科"
    },
    {
        "name": "小红",
        "school": "清北",
        "type": "本科"
    },
    {
        "name": "小花",
        "school": "清北",
        "type": "本科"
    },
    {
        "name": "小明",
        "school": "清北",
        "type": "本科"
    },
    {
        "name": "小米",
        "school": "浙大",
        "type": "研究生"
    },
    {
        "name": "小红",
        "school": "浙大",
        "type": "研究生"
    },
    {
        "name": "小花",
        "school": "浙大",
        "type": "研究生"
    },
    {
        "name": "小明",
        "school": "浙大",
        "type": "研究生"
    },
    {
        "name": "小米",
        "school": "复旦",
        "type": "大专"
    },
    {
        "name": "小红",
        "school": "复旦",
        "type": "大专"
    },
    {
        "name": "小花",
        "school": "复旦",
        "type": "大专"
    },
    {
        "name": "小明",
        "school": "复旦",
        "type": "大专"
    },
    {
        "name": "小米",
        "school": "剑桥",
        "type": "硕士"
    },
    {
        "name": "小红",
        "school": "剑桥",
        "type": "硕士"
    },
    {
        "name": "小花",
        "school": "剑桥",
        "type": "硕士"
    },
    {
        "name": "小明",
        "school": "剑桥",
        "type": "硕士"
    }
]

js如何实现数组合并(concat函数用法)

标签: js数组合并, js数组转换

上面是“js用最简单的方法实现2个数组的交叉一对一合并”的全面内容,想了解更多关于 js 内容,请继续关注web建站教程。

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

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

当前位置: 网站首页 > js
本文共计922个字,预计阅读时长7分钟
生活小工具,收录了80多款小工具
上一篇: 推荐一个免费、无版权的古典音乐下载的网站——Musopen
下一篇: 推荐一款免费在线图片处理工具——佐糖图片平台
x 打工人ai神器