Javascript 如何从另一个数组中减少一个数组的值
我想从带有诱饵图片的阵列中减少带有常规图片的阵列。正确的方法是什么 我的代码: 具有常规图片的阵列:Javascript 如何从另一个数组中减少一个数组的值,javascript,arrays,loops,react-native,Javascript,Arrays,Loops,React Native,我想从带有诱饵图片的阵列中减少带有常规图片的阵列。正确的方法是什么 我的代码: 具有常规图片的阵列: <View style={{flexDirection: 'row', width: width, flexWrap: 'wrap', height: photoWidth*2}}> { this.state.photos.map((p, i) => { re
<View style={{flexDirection: 'row', width: width, flexWrap: 'wrap', height: photoWidth*2}}>
{
this.state.photos.map((p, i) => {
return (
<SelectedPhoto
key={i}
index={i}
style={{
width: photoWidth,
height: photoWidth,
}}
limit={this.state.supportLength}
photo={p}
onSelectPhoto={this.onSelectPhoto}
onDeselectPhoto={this.onDeselectPhoto}
/>
);
})
}
</View>
<View style={{flexDirection: 'row', width: width, flexWrap: 'wrap', marginBottom:40}}>
{
this.state.decoyPhotos.map((p, i) => {
return (
<SelectedPhoto
key={i}
index={i}
style={{
width: photoWidth,
height: photoWidth,
}}
limit={this.state.supportLength}
photo={p}
onSelectPhoto={this.onSelectPhoto}
onDeselectPhoto={this.onDeselectPhoto}
/>
);
})
}
</View>
我想从带有诱饵图片的阵列中减少带有常规图片的阵列。正确的方法是什么 用Array.prototype.reduce()试试。它被描述了 以下是mozilla的一个示例:
const array1 = [1, 2, 3, 4];
const reducer = (accumulator, currentValue) => accumulator + currentValue;
// 1 + 2 + 3 + 4
console.log(array1.reduce(reducer));
// expected output: 10
// 5 + 1 + 2 + 3 + 4
console.log(array1.reduce(reducer, 5));
// expected output: 15
那么@HyyanAboFakher呢?是的,我试图使用这个函数,但我得到了语法错误。你能给我一个适合我的代码的例子吗?我知道这个函数,但我不能用我的语法让它工作。你能提供一个与我的语法相关的代码吗?
const array1 = [1, 2, 3, 4];
const reducer = (accumulator, currentValue) => accumulator + currentValue;
// 1 + 2 + 3 + 4
console.log(array1.reduce(reducer));
// expected output: 10
// 5 + 1 + 2 + 3 + 4
console.log(array1.reduce(reducer, 5));
// expected output: 15