a543462378 2022-04-27 15:09 采纳率: 84.7%
浏览 241
已结题

react中如何把后端返回的数组合并入前端定义的数组

项目中前端自定义了数组1,然后访问后台接口获得数组二,要根据shipType参数把两个数组合并成一个

// 前端定义的数组1
const tableList = [
    { shipType: "0", useFrequency: 0, useDuration: 0, useAmount: 0 },
    { shipType: "1", useFrequency: 0, useDuration: 0, useAmount: 0 },
    { shipType: "2", useFrequency: 0, useDuration: 0, useAmount: 0 },
    { shipType: "3", useFrequency: 0, useDuration: 0, useAmount: 0 },
    { shipType: "4", useFrequency: 0, useDuration: 0, useAmount: 0 },
    { shipType: "5", useFrequency: 0, useDuration: 0, useAmount: 0 },
    { shipType: "6", useFrequency: 0, useDuration: 0, useAmount: 0 },
    { shipType: "7", useFrequency: 0, useDuration: 0, useAmount: 0 },
    { shipType: "8", useFrequency: 0, useDuration: 0, useAmount: 0 },
    { shipType: "9", useFrequency: 0, useDuration: 0, useAmount: 0 },
  ]
{
  "code":"0",
  "data":{
      "detailedList":[],
      "list":[
            { shipType: "3", useFrequency: 9, useDuration: 34, useAmount: 6 },
            { shipType: "5", useFrequency: 8, useDuration: 0, useAmount: 12 },
      ],
      "logInfo":[],
      "msg":"成功"}

我写了几个方法都不成功

// 方法一
let tableLists = tableList.map(item => item.shipType === res.data.list.shipType ? res.data.list : item)
        console.log(tableLists)
// 输出结果和数组1完全一样
// 方法二
        for(let i=0;i<tableList.length;i++){
          if(tableList[i].shipType === list1.shipType){
            tableList[i] = list1
          }
        }
// 输出结果和数组1完全一样

要怎么写?或者我写的方法有什么不对的地方?

  • 写回答

3条回答 默认 最新

  • Heerey525 前端领域新星创作者 2022-04-27 15:37
    关注
    
                const tableList = [
                    { shipType: "0", useFrequency: 0, useDuration: 0, useAmount: 0 },
                    { shipType: "1", useFrequency: 0, useDuration: 0, useAmount: 0 },
                    { shipType: "2", useFrequency: 0, useDuration: 0, useAmount: 0 },
                    { shipType: "3", useFrequency: 0, useDuration: 0, useAmount: 0 },
                    { shipType: "4", useFrequency: 0, useDuration: 0, useAmount: 0 },
                    { shipType: "5", useFrequency: 0, useDuration: 0, useAmount: 0 },
                    { shipType: "6", useFrequency: 0, useDuration: 0, useAmount: 0 },
                    { shipType: "7", useFrequency: 0, useDuration: 0, useAmount: 0 },
                    { shipType: "8", useFrequency: 0, useDuration: 0, useAmount: 0 },
                    { shipType: "9", useFrequency: 0, useDuration: 0, useAmount: 0 },
                ]
    
                const list = [
                    { shipType: "3", useFrequency: 9, useDuration: 34, useAmount: 6 },
                    { shipType: "5", useFrequency: 8, useDuration: 0, useAmount: 12 }
                ]
    
                for (let i = 0; i < list.length; i++) {
                    for (let j = 0; i < tableList.length; j++) {
                        if (list[i].shipType === tableList[j].shipType) {
                            tableList[j] = list[i]
                            break
                        }
                    }
                }
                console.log('tableList', tableList)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 5月5日
  • 已采纳回答 4月27日
  • 创建了问题 4月27日

悬赏问题

  • ¥30 频率分析法分析绘制奈奎斯特图、波特图
  • ¥15 弹出来一万个系统找不到指定的文件框框,怎么解决
  • ¥15 ADS生成的微带线为什么是蓝色空心的
  • ¥15 求一下解题思路,完全不懂
  • ¥15 tensorflow
  • ¥15 densenet网络结构中,特征以cat方式复用后是怎么进行误差回传的
  • ¥15 STM32G471芯片spi设置了8位,总是发送16位
  • ¥15 R语言并行计算beta-NTI中tree文件的类型
  • ¥15 如何解读marsbar导出的ROI数据?
  • ¥20 求友友协助弄一下基于STC89C52单片机的声光控制灯原理图