Javascript 不使用for循环的算法问题求解
我正在尝试解决下一个案例而不使用for循环。。。这意味着尝试只使用过滤、减少、映射或它们的组合 我得到了一组对象:Javascript 不使用for循环的算法问题求解,javascript,arrays,reactjs,algorithm,dictionary,Javascript,Arrays,Reactjs,Algorithm,Dictionary,我正在尝试解决下一个案例而不使用for循环。。。这意味着尝试只使用过滤、减少、映射或它们的组合 我得到了一组对象: const项= [{label:1,count:22,isRefined:false} ,{label:2,count:10,isRefined:false} ,{label:3,count:3,isRefined:false} ,{label:4,count:1,isRefined:false} ] 常数乘积= [{位置:[{LocationType:1},{LocationTy
const项=
[{label:1,count:22,isRefined:false}
,{label:2,count:10,isRefined:false}
,{label:3,count:3,isRefined:false}
,{label:4,count:1,isRefined:false}
]
常数乘积=
[{位置:[{LocationType:1},{LocationType:2},{LocationType:3}]}
,{位置:[{LocationType:1},{LocationType:3}]}
,{LocationType:[{LocationType:1},{LocationType:3},{LocationType:4}]}
,{Locations:[{LocationType:1}]}
,{LocationType:[{LocationType:1},{LocationType:2},{LocationType:3}]}
]
接下来的情况是:
我需要用相应产品的值覆盖每个项目的内部对象计数键,其中位置对象包含与项目对象标签匹配的位置类型
例如,这里的输出应该是
const输出=
[{label:1,count:5,isRefined:false}
,{label:2,count:2,isRefined:false}
,{label:3,count:4,isRefined:false}
,{label:4,count:1,isRefined:false}
]
我完全被困在试图找到一个解决方案,而不使用for循环或任何形式的for循环,帮助将不胜感激!谢谢首先计算计数(每个位置类型出现在
产品
数组中的次数),最好作为一个对象,其中键是位置类型,值是计数。为此,一个简单的reduce
/forEach
将完成如下工作:
let counts = products.reduce((counts, locs) => {
locs.Locations.forEach(loc =>
counts[loc.LocationType] = (counts[loc.LocationType] || 0) + 1
);
return counts;
}, {});
然后简单地map
将项
数组映射到一个新数组中,在该数组中使用扩展语法浅复制对象,并使用前面计算的计数
对象更新计数
属性:
const newItems = items.map(item => ({
...item,
count: counts[item.label] || 0
}));
如果您想改变原始对象本身,那么一个简单的forEach
就足够了:
items.forEach(item => item.count = counts[item.label] || 0);
如果项目在产品
数组中没有任何位置类型,则| | 0
部分是一个回退值,在这种情况下,计数[项目.标签]
将是未定义的
,而将使用0
。这称为短路评估
演示:
const items=[{label:1,count:22,isRefined:false},{label:2,count:10,isRefined:false},{label:3,count:3,isRefined:false},{label:4,count:1,isRefined:false}];
const products=[{LocationType:1},{LocationType:2},{LocationType:3}]},{LocationType:1},{LocationType:3}}},{LocationType:1},{LocationType:3},{LocationType:4},{LocationType:1},{LocationType:1},{LocationType:2},{LocationType:3};
让计数=产品。减少((计数,LOC)=>{
locs.LOCITIONS.forEach(loc=>
计数[loc.LocationType]=(计数[loc.LocationType]| | 0)+1
);
返回计数;
}, {});
const newItems=items.map(item=>({
…项目,
计数:计数[项目.标签]| | 0
}));
console.log(newItems)代码>简单、优雅
更新
感谢您在下面的评论中建议使用some()。那样真是太好了
const output = items.map(item => {
const matches = (location) => location.LocationType == item.label;
const filtered = products.filter(({ Locations }) => Locations.some(matches));
return { ...item, count: filtered.length };
});
如果forEach不是一个选项,下面是另一个使用map、reduce和filter的解决方案
const result = items.map(d => {
const locationCount = products.reduce((count, p) => count + p.Locations.filter(l => l.LocationType == d.label).length, 0);
let item = Object.assign({}, d);
item.count = locationCount;
return item;
})
例如:
const项=
[{label:1,count:22,isRefined:false}
,{label:2,count:10,isRefined:false}
,{label:3,count:3,isRefined:false}
,{label:4,count:1,isRefined:false}
]
常数乘积=
[{位置:[{LocationType:1},{LocationType:2},{LocationType:3}]}
,{位置:[{LocationType:1},{LocationType:3}]}
,{LocationType:[{LocationType:1},{LocationType:3},{LocationType:4}]}
,{Locations:[{LocationType:1}]}
,{LocationType:[{LocationType:1},{LocationType:2},{LocationType:3}]}
]
const result=items.map(d=>{
const locationCount=products.reduce((count,p)=>count+p.Locations.filter(l=>l.LocationType==d.label)。长度,0);
让item=Object.assign({},d);
item.count=位置计数;
退货项目;
})
控制台日志(结果)代码>
.as控制台包装{最大高度:100%!重要;顶部:0;}
我的方式
const countP=v=>products.filter(p=>p.Locations.some(x=>x.LocationType==v)).length
const output=items.map(el=>({…el,count:countP(el.label)}))
证明:
const项=
[{label:1,count:22,isRefined:false}
,{label:2,count:10,isRefined:false}
,{label:3,count:3,isRefined:false}
,{label:4,count:1,isRefined:false}
]
常数乘积=
[{位置:[{LocationType:1},{LocationType:2},{LocationType:3}]}
,{位置:[{LocationType:1},{LocationType:3}]}
,{LocationType:[{LocationType:1},{LocationType:3},{LocationType:4}]}
,{Locations:[{LocationType:1}]}
,{LocationType:[{LocationType:1},{LocationType:2},{LocationType:3}]}
]
const countP=v=>products.filter(p=>p.Locations.some(x=>x.LocationType==v)).length
const output=items.map(el=>({…el,count:countP(el.label)}))
console.log(输出)
.as控制台包装{最大高度:100%!重要;顶部:0;}
.作为控制台行{背景色:#87f1f1;}
.as console row::在{display:none!important;}
Byfor
loop之后,您的意思是专门针对(让我=