如何减少另一个数组的数组值

时间:2018-07-10 13:31:08

标签: javascript arrays loops react-native

我想从带有诱饵图片的阵列中减少带有常规图片的阵列。正确的做法是什么?

我的代码:

带有常规图片的阵列:

<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>

我的值存储在这里:

constructor(props) {
    super(props);

    this.selectedDecoy=[];
    this.selectedPhotos = [];
    this.selectedPhotosIndex = 0;

    let photos = this.getSelectedPhotos();

    this.state = {
        decoyPhotos: null,
        photos: photos,
        loader: {
            loading: 1,
            message: "Loading photos..."
        },
        supportLength: photos.length,
        selectedDecoy: [],
        step: 0,
        progress: new Animated.Value(0),
        animationRunning: false
    };

我想从带有诱饵图片的阵列中减少带有常规图片的阵列。正确的做法是什么?

1 个答案:

答案 0 :(得分:0)

尝试使用Array.prototype.reduce()。描述为here

这是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