一维数组循环按条件合并给二维数组的问题,逻辑实在想不通了,请教各位大神?

小程序 文章 2022-04-11 11:40 453 0 全屏看文

AI助手支持GPT4.0

一维数组循环按条件合并给二维数组的问题,逻辑实在想不通了,请教各位大神?The problem of merging one-dimensional array loops into two-dimensional arrays according to conditions, the logic is really hard to figure out, please ask the gods?

一维数组循环按条件合并给二维数组的问题,逻辑实在想不通了,请教各位大神?
//数组1var sensorList:[      {"id":10000,"category""sensor","name""温度","value""1"},      {"id":10001,"category""sensor","name""湿度","value""2"},      {"id":10002,"category""sensor","name""气压","value""3"},      {"id":10003,"category""sensor","name""光照","value""4"},      {"id":10004,"category""sensor","name""风速","value""5"},      {"id":10005,"category""sensor","name""雨量","value""6"},      {"id":10006,"category""sensor","name""PM2.5","value""20"},      {"id":10007,"category""sensor","name""PM10","value""21"},      {"id":10008,"category""sensor","name""风向","value""37"},      {"id":10009,"category""sensor","name""风向","value""38"},      {"id":10000,"category""sensorUnit","name""℃","value""1"},      {"id":10101,"category""sensorUnit","name""%","value""2"},      {"id":10102,"category""sensorUnit","name""kPa","value""3"},      {"id":10103,"category""sensorUnit","name""kLux","value""4"},      {"id":10104,"category""sensorUnit","name""m/s","value""5"},      {"id":10105,"category""sensorUnit","name""mm/h","value""6"},      {"id":10106,"category""sensorUnit","name""ug/m3","value""20"},      {"id":10107,"category""sensorUnit","name""ug/m3","value""21"},      {"id":10108,"category""sensorUnit","name""°","value""37"},      {"id":10109,"category""sensorUnit","name""","value""38"},      {"id":10002,"category""windDir","name""北","value""16"},      {"id":10003,"category""windDir","name""北偏东","value""1"},      {"id":10004,"category""windDir","name""东北","value""2"},      {"id":10005,"category""windDir","name""东偏北","value""3"},      {"id":10006,"category""windDir","name""东","value""4"},      {"id":10007,"category""windDir","name""东偏南","value""5"},      {"id":10008,"category""windDir","name""东南","value""6"},      {"id":10009,"category""windDir","name""南偏东","value""7"},      {"id":10000,"category""windDir","name""南","value""8"},      {"id":10101,"category""windDir","name""南偏西","value""9"},      {"id":10102,"category""windDir","name""西南","value""10"},      {"id":10103,"category""windDir","name""西偏南","value""11"},      {"id":10104,"category""windDir","name""西","value""12"},      {"id":10105,"category""windDir","name""西偏北","value""13"},      {"id":10106,"category""windDir","name""西北","value""14"},      {"id":10107,"category""windDir","name""北偏西","value""15"},      {"id":10108,"category""windDir","name""静风","value""17"},      {"id":10109,"category""windDir","name""静风","value""18"}    ],//数组2   var monitorList:[      {        id: 10001382,        name: "测试01",        listsensor:[        {monitorid:10001382,sensorid:1,value: 11.1},        {monitorid:10001382,sensorid:2,value: 22.2},        {monitorid:10001382,sensorid:37,value: 180},        {monitorid:10001382,sensorid:38,value: 2}        ]},      {        id: 10001383,        name: "测试02",        listsensor:[        {monitorid:10001383,sensorid:3,value: 11.1},        {monitorid:10001383,sensorid:4,value: 22.2},        {monitorid:10001383,sensorid:37,value: 180},        {monitorid:10001383,sensorid:38,value: 2}        ]}  ]//需求结果/* newList:[      {id: 10001382,name: "测试01",listsensor:[        {monitorid:10001382,sensorid:1,value: 11.1,name:"温度",unit:"℃"},        {monitorid:10001382,sensorid:2,value: 22.2,name:"湿度",unit:"%"},        {monitorid:10001382,sensorid:37,value: 180,name:"风向",unit:"°"},        {monitorid:10001382,sensorid:38,value: 4,name:"风向",unit:"东"}      ]},      {id: 10001383,name: "测试02",listsensor:[        {monitorid:10001383,sensorid:3,value: 11.1,name:"气压",unit:"kPA"},        {monitorid:10001383,sensorid:4,value: 22.2,name:"光照",unit:"kLux"},        {monitorid:10001383,sensorid:37,value: 180,name:"风向",unit:"°"},        {monitorid:10001383,sensorid:38,value: 2,name:"风向",unit:"东北"}      ]}] */

//数组1var sensorList:[      {"id":10000,"category": "sensor","name": "温度","value": "1"},      {"id":10001,"category": "sensor","name": "湿度","value": "2"},      {"id":10002,"category": "sensor","name": "气压","value": "3"},      {"id":10003,"category": "sensor","name": "光照","value": "4"},      {"id":10004,"category": "sensor","name": "风速","value": "5"},      {"id":10005,"category": "sensor","name": "雨量","value": "6"},      {"id":10006,"category": "sensor","name": "PM2.5","value": "20"},      {"id":10007,"category": "sensor","name": "PM10","value": "21"},      {"id":10008,"category": "sensor","name": "风向","value": "37"},      {"id":10009,"category": "sensor","name": "风向","value": "38"},      {"id":10000,"category": "sensorUnit","name": "℃","value": "1"},      {"id":10101,"category": "sensorUnit","name": "%","value": "2"},      {"id":10102,"category": "sensorUnit","name": "kPa","value": "3"},      {"id":10103,"category": "sensorUnit","name": "kLux","value": "4"},      {"id":10104,"category": "sensorUnit","name": "m/s","value": "5"},      {"id":10105,"category": "sensorUnit","name": "mm/h","value": "6"},      {"id":10106,"category": "sensorUnit","name": "ug/m3","value": "20"},      {"id":10107,"category": "sensorUnit","name": "ug/m3","value": "21"},      {"id":10108,"category": "sensorUnit","name": "°","value": "37"},      {"id":10109,"category": "sensorUnit","name": "","value": "38"},      {"id":10002,"category": "windDir","name": "北","value": "16"},      {"id":10003,"category": "windDir","name": "北偏东","value": "1"},      {"id":10004,"category": "windDir","name": "东北","value": "2"},      {"id":10005,"category": "windDir","name": "东偏北","value": "3"},      {"id":10006,"category": "windDir","name": "东","value": "4"},      {"id":10007,"category": "windDir","name": "东偏南","value": "5"},      {"id":10008,"category": "windDir","name": "东南","value": "6"},      {"id":10009,"category": "windDir","name": "南偏东","value": "7"},      {"id":10000,"category": "windDir","name": "南","value": "8"},      {"id":10101,"category": "windDir","name": "南偏西","value": "9"},      {"id":10102,"category": "windDir","name": "西南","value": "10"},      {"id":10103,"category": "windDir","name": "西偏南","value": "11"},      {"id":10104,"category": "windDir","name": "西","value": "12"},      {"id":10105,"category": "windDir","name": "西偏北","value": "13"},      {"id":10106,"category": "windDir","name": "西北","value": "14"},      {"id":10107,"category": "windDir","name": "北偏西","value": "15"},      {"id":10108,"category": "windDir","name": "静风","value": "17"},      {"id":10109,"category": "windDir","name": "静风","value": "18"}    ],//数组2   var monitorList:[      { id: 10001382, name: "测试01", listsensor:[        {monitorid:10001382,sensorid:1,value: 11.1},        {monitorid:10001382,sensorid:2,value: 22.2},        {monitorid:10001382,sensorid:37,value: 180},        {monitorid:10001382,sensorid:38,value: 2}       ]},     { id: 10001383, name: "测试02", listsensor:[        {monitorid:10001383,sensorid:3,value: 11.1},        {monitorid:10001383,sensorid:4,value: 22.2},        {monitorid:10001383,sensorid:37,value: 180},        {monitorid:10001383,sensorid:38,value: 2}       ]} ]//需求结果/* newList:[ {id: 10001382,name: "测试01",listsensor:[        {monitorid:10001382,sensorid:1,value: 11.1,name:"温度",unit:"℃"},        {monitorid:10001382,sensorid:2,value: 22.2,name:"湿度",unit:"%"},        {monitorid:10001382,sensorid:37,value: 180,name:"风向",unit:"°"},        {monitorid:10001382,sensorid:38,value: 4,name:"风向",unit:"东"}      ]},      {id: 10001383,name: "测试02",listsensor:[        {monitorid:10001383,sensorid:3,value: 11.1,name:"气压",unit:"kPA"},        {monitorid:10001383,sensorid:4,value: 22.2,name:"光照",unit:"kLux"},        {monitorid:10001383,sensorid:37,value: 180,name:"风向",unit:"°"},        {monitorid:10001383,sensorid:38,value: 2,name:"风向",unit:"东北"}      ]}] */

回答:

HardPan:

我的方法:

先定义一维数组需要的数据:

var var newArr = [];let newListsensor = [];var arr1 = this.data.sensorList.filter(item=>{return item.category==="sensor"})var arr2 = this.data.sensorList.filter(item=>{return item.category==="sensorUnit"})for(let i=0;i<arr1.length;i++){            let obj = arr1[i],num=obj.value,flag=false;            for(let j=0;j<arr2.length;j++){                let aj = arr2[j],n=aj.value,yearObj={};                if(n==num){                    flag=true;                    yearObj["sensorid"]=num||n;                    yearObj["name"] = obj.name;                    yearObj["unit"] = aj.name;                    newArr.push(yearObj);                }            }}  //得到新数组  /*newArr:[  {sensorid: "1", name: "温度", unit: "℃"},  {sensorid: "2", name: "湿度", unit: "%"},  {sensorid: "3", name: "气压", unit: "kPa"},  {sensorid: "4", name: "光照", unit: "kLux"},  {sensorid: "5", name: "风速", unit: "m/s"},  {sensorid: "6", name: "雨量", unit: "mm/h"},  {sensorid: "20", name: "PM2.5", unit: "ug/m3"},  {sensorid: "21", name: "PM10", unit: "ug/m3"},  {sensorid: "37", name: "风向", unit: "°"},  {sensorid: "38", name: "风向", unit: ""}  ]for(let i=0;i<monitorList.length;i++){  newListsensor.push(monitorList[i].listsensor)}

/*newListsensor:[

]

push成一个二维数组之后怎么跟newArr,通过“sensorid”进行数据合并呢?


武曲心:

如果要做数据提交的话就做循环来组装数据,如果只是用来渲染页面的话用通过sensorid用wxs匹配查找sensorList来实现

HardPan:是用来做数据渲染的,最后取结果的时候是把整个取到的数组内容渲染出来了。
武曲心:
嘿ོ,我ོ就ོ是那个小伙🍊ོ:

你这两个数据结构就不合理,关联性极差,就算合成估计也会有数据错乱

HardPan:我的想法是先把一维数组里面的结构合并一下,在给二维数组循环按照条件合并。但是结果不太理想。需要循环太多次了

-EOF-

AI助手支持GPT4.0


国内超级便宜服务器

摸鱼人热门新闻聚合

钻级赞助商 我要加入

开发者在线工具

第三方支付技术请加QQ群

相关文章
从视频号主页如何跳转到自己公司的小程序?
小程序 wx.navigateBack 返回上一级 带入图片参数 上一级 无法渲染在页面上?
客户在小商店购物,确认收货后,不能评价?
店铺运营?
体验版小程序点击浮窗后,微信秒退,之后无法进入,只能卸载重新安装
随便看看
微信设置不可以使用小程序(游戏小程序) 5635
adsense申请已经支付的订单回退的网址 3212
BT面板查看网站入口、账号密码的命令 6009
ucloud海外服务器超级优惠券 3274
根据您的消费情况,您的延停权益已发生变化,变更后的延停权益为延停额度20.00元 5831
为什么我们喜欢迈克尔杰克逊。 4164
阿里云com、cn域名续费优惠码(2020年5月可用) 3586
阿里云com、cn域名续费优惠码(2020年6月可用) 3606
微信公众号开五联增值税发票怎么处理 2950
仙之侠道幻彩衣的合成和属性预览 4316