需求如下:letshowArr=[{malltype:'旗舰店',symbol:'R标',region:'华南地区',industry:'美容护理',price:'5万-10万'},{malltype:'旗舰店',symbol:'R标',region:'华南地区',industry:'服饰鞋包',price:'5万-10万'},{malltype:'旗舰店',symbol:'R标',region:'华南地区',industry:'游戏话费',price:'5万-10万'}]//筛选对象letreqArr=['旗舰店','服饰鞋包']//筛选条件value,且不确定对应的属性输出:[{malltype:'旗舰店',symbol:'R标',region:'华南地区',industry:'服饰鞋包',price:'5万-10万'}]目前已有的一个思路:lettemp=[];letcache;reqArr.forEach(function(item,index){showArr.forEach(function(sub){cache=Object.values(sub)cache.forEach(function(part,count){if(part==item){//console.log(item)temp=[];temp.push(sub)}})})})console.log(temp);
2 回答
翻过高山走不出你
TA贡献1875条经验 获得超3个赞
constpick=(source,values)=>source.filter(srcItem=>values.every(val=>Object.values(srcItem).includes(val)))pick(showArr,reqArr)
当年话下
TA贡献1890条经验 获得超9个赞
functionfind(source,target){varret=[];if(Array.isArray(source)&&Array.isArray(target)){if(!target.length)returnsource;for(vari=0;ivarobj=source[i]; vararr=target.slice();for(varkeyinobj){varval=obj[key];varindex=arr.indexOf(val);if(indexinarr)arr.splice(index,1);}if(!arr.length)ret.push(obj);}}returnret;}console.log(find(showArr,reqArr));
添加回答
举报
0/150
提交
取消