用for循环减少相似的值

时间:2018-07-12 12:39:17

标签: javascript arrays loops react-native

我有一个简单的JavaScript代码,为减少2个数组中的相似值而编写,该代码本身运行良好:

JavaScript代码:

 var Array2 = [ "1", "2", "3", "4", "5" ]; 
    var Array1 = ["1","2","3"];

    var result = Array2.filter(val => !Array1.includes(val));
    //var result = [];


    for(var i = 0; i < Array1.length ; i++){

        var x = Array1[i];
        var check = false;        

        for( var y = 0; y < Array2.length; y++){

          if(x == Array2[y]){


            check = true;

          }
        }
        if(!check){
          result.push(x);
        }
    }
    console.log(result);

我面临的问题是在我的应用程序中实现相同的逻辑,在该应用程序中,我有2张图片,而我不希望有选择相似图片的选项。

我的onSelectPhoto函数(可能有很多错误):

onSelectPhoto = (photo, callback) => {

        let photos = new Set([...this.selectedDecoy]);
        let len = this.selectedPhotosIndex; 
        let decoyPhotos = this.state.decoyPhotos;


        //let result = decoyPhotos.filter(val => 
        !photos.includes(val));

        for(var i = 0; i < photos.length ; i++){

            var x = photos[i];
            var check = false;        

            for( var y = 0; y < decoyPhotos.length; y++){

              if(x == decoyPhotos[y]){


                check = true;

              }
            }
            if(!check){
              result.push(x);
            console.log('photos',photos); 


        }

        if (len > this.state.supportLength - 1) {           
            this.limitDialog.open();
        }
        else if (len === (this.state.supportLength - 1)) {
            photos.add(photo);
            this.selectedDecoy = Array.from(photos);
            this.selectedPhotosIndex = this.selectedPhotosIndex + 1;
            callback(true);

        }
        else {
            this.selectedPhotosIndex = this.selectedPhotosIndex + 1;
            photos.add(photo);
            this.selectedDecoy = Array.from(photos);
            callback(true);
        }
        //else {
        //  photos.add(photo);
        //  this.setState({selectedDecoy: Array.from(photos)});
        //  this.props.setSelectedPhotos(len);
        //}
    }
}  

该函数无效,并返回this.selectedDecoy作为空数组和this.selectedPhotosIndex作为0。

我的变量的声明:

constructor(props) {
        super(props);

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


        let photos = this.getSelectedPhotos();

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

        this.props.navigation.setParams({notificationAction: this.onNotification, isIncome: this.props.isNewNotifications});
    }

我的渲染:

renderLoader(){

                {
                    this.state.photos.map((p, i) => {
                        console.log('this.state.photos.', this.state.photos)
                        return (
                            <SelectedPhoto
                                key={i}
                                index={i}
                                style={{
                                    width: photoWidth,
                                    height: photoWidth,

                                }}
                                limit={this.state.supportLength}
                                photo={p}
                                onSelectPhoto={this.onSelectPhoto}
                                onDeselectPhoto={this.onDeselectPhoto}
                            />
                        );
                    })
                }



                    {
                        this.state.decoyPhotos.map((p, i) => {
                            console.log('this.state.decoyPhotos', this.state.decoyPhotos)
                            return (
                                <SelectedPhoto
                                    key={i}
                                    index={i}
                                    style={{
                                        width: photoWidth,
                                        height: photoWidth,
                                    }}
                                    limit={this.state.supportLength}
                                    photo={p}
                                    onSelectPhoto={this.onSelectPhoto}
                                    onDeselectPhoto={this.onDeselectPhoto}
                                />
                            );
                        })
                    }

                {
                    this.state.photos.map((p, i) => {
                        console.log('this.state.photos.', this.state.photos)
                        return (
                            <SelectedPhoto
                                key={i}
                                index={i}
                                style={{
                                    width: photoWidth,
                                    height: photoWidth,
                                }}
                                limit={this.state.supportLength}
                                photo={p}
                            />
                );
                    })
                }

完整代码: 1. https://pastebin.com/swKj1WQZ 2. SelectedPhoto.js:https://pastebin.com/pm1tjBsd

0 个答案:

没有答案