【实习小tip】三维数组对象和一维数组对象互相转化

25 篇文章 0 订阅
23 篇文章 0 订阅

三维数组对象转化为一维数组对象

需要把一个三维数组对象用elementUI的表格渲染,想要做到首先要把它转为我所需要的一维数组对象,方法简单粗暴

前后效果对比

参考文章

QualityManageApi.get(this.params.id).then((res) => {
const data = res.data
this.form = data
let data1 = data.voList
data1 = Array.from(data1);
function getEachData1 (obj) {  
  return obj.items.map((item)=>{  
	let data = {  
	  ...item,
	  ...obj  
	};  
	delete data.items;  
	return data;  
  });  
}  
function getEachData2 (obj) {
  return obj.standards.map((item)=>{  
	let data = {  
	  ...item,
	  ...obj  
	};  
	delete data.standards;  
	return data;  
  });  
}    
const newData1 = []; 
const newData2 = []; 
data1.map((obj)=>{  
  newData1.push(...getEachData1(obj));  
});
newData1.map((obj)=>{
  newData2.push(...getEachData2(obj));  
});
console.log(data1);
console.log(newData2);

一维对象数组转为三维数组

参考文章

https://blog.csdn.net/weixin_34365635/article/details/93612598?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-6.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-6.control

最后用的这个方法(很好用,重点在于可以把具有相同属性的对象拆分成几个子对象的父对象,很符合我的业务需求)

https://www.jb51.net/article/232810.htm

效果

修改前

在这里插入图片描述

修改后

在这里插入图片描述

let list = Array.from(new Set(
    this.voListForm.map(item => {
        return item['typeName']
    })))
let subList = []
list.forEach(res => {
    this.voListForm.forEach(ele => {
        if (ele['typeName'] === res) {
            let nameArr = subList.map(item => item.typeName)
            if (nameArr.indexOf(res) !== -1) {
                let nameArr2 = subList[nameArr.indexOf(res)].items.map(item => item.typeName)
                if (nameArr2.indexOf(ele['itemName']) !== -1) {
                    subList[nameArr.indexOf(res)].items[nameArr2.indexOf(ele['itemName'])].standards.push({
                        deductScore: ele['deductScore'],
                        recordsId: ele['recordsId'],
                        standard: ele['standard'],
                    })
                } else {
                    subList[nameArr.indexOf(res)].items.push({
                        example: ele['example'],
                        itemId: ele['itemId'],
                        itemName: ele['itemName'],
                        score: ele['score'],
                        standards: [{
                            deductScore: ele['deductScore'],
                            recordsId: ele['recordsId'],
                            standard: ele['standard'],
                        }]
                    })
                }
            } else {
                subList.push({
                    totalScore: ele['totalScore'],
                    typeId: ele['typeId'],
                    typeName: ele['typeName'],
                    items: [{
                        example: ele['example'],
                        itemId: ele['itemId'],
                        itemName: ele['itemName'],
                        score: ele['score'],
                        standards: [{
                            deductScore: ele['deductScore'],
                            recordsId: ele['recordsId'],
                            standard: ele['standard'],
                        }]
                    }]
                })
            }
        }
    })

})
console.log(subList)

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,让我逐个回答你的问题: 1. 使用C语言生成一个性质较好的二维数组并返回它的函数: ```c #include <stdlib.h> #include <time.h> int** Create(int rows, int cols){ int** array = (int**)malloc(rows * sizeof(int*)); for(int i = 0; i < rows; i++){ array[i] = (int*)malloc(cols * sizeof(int)); for(int j = 0; j < cols; j++){ // 使用随机数生成 0 或 1,使得数组中的元素大概率为 0,小概率为 1 array[i][j] = rand() % 5 == 0 ? 1 : 0; } } return array; } ``` 2. 计算迷宫的所有路径并给出最佳路径的函数: ```c #include <stdbool.h> #include <stdio.h> bool Check(int** maze, int rows, int cols, int x, int y, int** visited, int** path, int* pathLength){ if(x < 0 || x >= rows || y < 0 || y >= cols || maze[x][y] == 1 || visited[x][y] == 1){ return false; } visited[x][y] = 1; path[*pathLength][0] = x; path[*pathLength][1] = y; (*pathLength)++; if(x == rows - 1 && y == cols - 1){ return true; } if(Check(maze, rows, cols, x + 1, y, visited, path, pathLength)){ return true; } if(Check(maze, rows, cols, x - 1, y, visited, path, pathLength)){ return true; } if(Check(maze, rows, cols, x, y + 1, visited, path, pathLength)){ return true; } if(Check(maze, rows, cols, x, y - 1, visited, path, pathLength)){ return true; } (*pathLength)--; visited[x][y] = 0; return false; } int** Solution(int** maze, int rows, int cols, int* pathLength){ int** visited = (int**)malloc(rows * sizeof(int*)); for(int i = 0; i < rows; i++){ visited[i] = (int*)malloc(cols * sizeof(int)); for(int j = 0; j < cols; j++){ visited[i][j] = 0; } } int** path = (int**)malloc(rows * cols * sizeof(int*)); for(int i = 0; i < rows * cols; i++){ path[i] = (int*)malloc(2 * sizeof(int)); } *pathLength = 0; bool hasPath = Check(maze, rows, cols, 0, 0, visited, path, pathLength); if(!hasPath){ printf("迷宫无解\n"); return NULL; } printf("迷宫有解,路径如下:\n"); for(int i = 0; i < *pathLength; i++){ printf("(%d, %d) ", path[i][0], path[i][1]); } printf("\n"); return path; } ``` 3. 给出下一步的最佳方案的函数: ```c int Tip(int** path, int pathLength, int x, int y){ for(int i = 0; i < pathLength - 1; i++){ if(path[i][0] == x && path[i][1] == y){ return path[i + 1][0] - x > 0 ? 0 : path[i + 1][0] - x < 0 ? 1 : path[i + 1][1] - y > 0 ? 2 : 3; } } return -1; } ``` 4. 输出最佳路径的函数: ```c void PrintPath(int** path, int pathLength){ printf("最佳路径为:\n"); for(int i = 0; i < pathLength; i++){ printf("(%d, %d) ", path[i][0], path[i][1]); } printf("\n"); } ``` 希望我的回答能够帮助到你!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值