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

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日

悬赏问题

  • ¥20 深信服vpn-2050这台设备如何配置才能成功联网?
  • ¥15 Arduino的wifi连接,如何关闭低功耗模式?
  • ¥15 Android studio 无法定位adb是什么问题?
  • ¥15 angular项目错误
  • ¥20 需要帮我远程操控一下,运行一下我的那个代码,我觉得我无能为力了
  • ¥20 有偿:在ubuntu上安装arduino以及其常用库文件。
  • ¥15 请问用arcgis处理一些数据和图形,通常里面有一个根据点划泰森多边形的命令,直接划的弊端是只能执行一个完整的边界,但是我们有时候会用到需要在有很多边界内利用点来执行划泰森多边形的命令
  • ¥30 在wave2foam中执行setWaveField时遇到了如下的浮点异常问题,请问该如何解决呢?
  • ¥750 关于一道数论方面的问题,求解答!(关键词-数学方法)
  • ¥200 csgo2的viewmatrix值是否还有别的获取方式