js 算法问题

有两个数组


    formThead: [

                {

                    label:'机会名称',

                    value:'oppoProjectName',

                    isShow:true

                },

                {

                    label:'客户名称',

                    value:'customerName',

                    isShow:true

                },

                {

                    label:'销售',

                    value:'saleName',

                    isShow:true

                }

            ]

            

            formTheadData:['oppoProjectName','customerName']

            

            如果在formThead中任何一位的value都不在formTheadData中   就把对用的formThead中的isShow变成false

            


互换的青春
浏览 497回答 5
5回答

千万里不及你

formThead.forEach((item) => {  item.isShow = formTheadData.includes(item.value);});

慕无忌1623718

更新 2let hash = formTheadData.reduce((p, c) => [p[c] = 0, p][1], {});// 得到 hash// {//     oppoProjectName: 0,//     customerName: 0// }formThead.forEach((item) => {    // 不使用 includes 来检索数据    //  item.isShow = formTheadData.includes(item.value);    item.isShow = !!hash[item.value]});这样就可以避免循环嵌套,性能可以有大幅提升。filter 是会返回一个新数组的,因此这个解法很有问题。非常抱歉,由于不是专职前端,这些细节容易忽略,误导题主和各位了。原答案:formThead.filter(h => formTheadData.indexOf(h.value) === -1).forEach(h => h.isShow = false)

芜湖不芜

var formThead= [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; label:'机会名称',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value:'oppoProjectName',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; isShow:true&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; label:'客户名称',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value:'customerName',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; isShow:true&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; label:'销售',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value:'saleName',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; isShow:true&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; formTheadData=['oppoProjectName','customerName']&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(var i=0;i<formThead.length;i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(var j=0;j<formTheadData.length;j++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(formThead[i].value==formTheadData[j]){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; formThead[i].isShow=true&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; formThead[i].isShow=false&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }

慕田峪7331174

formThead.map((item) => {&nbsp; &nbsp; item.isShow = false;&nbsp; &nbsp; formTheadData.some((el,index) => {&nbsp; &nbsp; &nbsp; &nbsp; if(item.value == el){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; item.isShow = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; formTheadData.splice(index,1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; })})

MM们

“如果在formThead中任何一位的value都不在formTheadData中 就把对用的formThead中的isShow变成false”楼主你能先解释下你这句话的意思吗?只从字面理解的话,只要formThead中的所有item的value都不在formTheadData中的话,就把formThead中所有的item的value变成false。但是貌似你又想表达formThead如果有item的value不存在formTheadData中的就把它的isShow置为false。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript