zoukankan      html  css  js  c++  java
  • 根据数组的某个属性判断是否存 并且转换为其他形式数组

    1.问题背景

    因element UI 中 级联选择器 选择 参数 表达为 

     [[0], [0, 1], [0, 2], [1, 0], [1, 1],[2,2]]
    不是我提交给后端想要的结果,所以需要转换
    2.主要过程
        var label = [
            {
                id: 0,
                name: '红色',
                isx: true,
                childen: [
                    {
                        id: 0,
                        name: '大',
                        isx: true,
                    },
                    {
                        id: 1,
                        name: '中',
                        isx: true,
    
                    },
                    {
                        id: 2,
                        name: '小',
                        isx: true,
    
                    }
                ]
            },
            {
                id: 1,
                name: '白色',
                childen: [
                    {
                        id: 0,
                        name: '大',
                        isx: true,
    
                    },
                    {
                        id: 1,
                        name: '中',
                        isx: true,
    
                    },
                    {
                        id: 2,
                        name: '小',
                    }
                ]
            },
            {
                id: 2,
                name: '绿色',
                childen: [
                    {
                        id: 0,
                        name: '大',
                    },
                    {
                        id: 1,
                        name: '中',
                    },
                    {
                        id: 2,
                        name: '小',
                    }
                ]
            }
        ]
        var arrFrom = [[0], [0, 1], [0, 2], [1, 0], [1, 1],[2,2]];
        let datas = [];
        const isBelowThreshold = (currentValue) => currentValue.id < 40;
        for (let i = 0; i < arrFrom.length; i++) {
            const datasIndex = (element) => element.val == arrFrom[i][0];
            const labelIndex = (element) => element.id == arrFrom[i][0];
            const ChildIndex = (element) => element.id == arrFrom[i][1];
            const newIndex = datas.findIndex(datasIndex);//新数组是否存在
            const oldIndex = label.findIndex(labelIndex);//原数组是否存在
            // 判断 datas数组 是否存在某个值    false
            if (newIndex === -1) {
                const childen = []
                //判断 arrFrom 值 是否存在第二个num
                if (arrFrom[i].length > 1) {
                    //查找 label数组子级中 存在 的索引
                    const cIndex = label[oldIndex].childen.findIndex(ChildIndex);
                    childens.isx = true;
                    childen.push(label[oldIndex].childen[cIndex]);
                }
                datas.push({
                    val: label[oldIndex].id,
                    name: label[oldIndex].name,
                    isx:label[oldIndex].isx?? null,
                    childen: childen
                })
            } else {
                const childen = []
                //判断 arrFrom 值 是否存在第二个num
                if (arrFrom[i].length > 1) {
                    //查找 label数组子级中 存在 的索引
                    const cIndex = label[oldIndex].childen.findIndex(ChildIndex);
                    childens = label[oldIndex].childen[cIndex];
                    childens.isx = true;
                    datas[newIndex].childen.push(childens)
                }
            }
        }
        console.log(datas)
  • 相关阅读:
    C# GetHashCode 部分冲突列表 数字字符串版本
    Amazon Dynamo DB
    SCTP 一句话介绍
    SystemTap 使用以及安装
    Windows Azure Service Disruption on Feb 29th
    发布ASP.NET MVC3网站
    SQLServer数据集合的交、并、差集运算
    DataTable的几个函数
    oracle创建表空间以及用户的语句
    asp.net mvc3及odp.net资料下载地址
  • 原文地址:https://www.cnblogs.com/zhaozhenghao/p/13629897.html
Copyright © 2011-2022 走看看