如何对promises数组进行promise.all?因为有时我的退货有时不正确

时间:2018-09-03 04:16:16

标签: javascript reactjs

如何对promises数组进行promise.all? 我在比较两个数组,但有时返回的结果不正确,我认为这是因为诺言不正确

有人可以帮助我吗?谢谢。

let newFilteredCategory = this.state.filteredCategory.slice();

console.log("newFilteredCategory", newFilteredCategory)

let mainFound = false;
let subfound = false;
let shouldDeleteMain = false;
let shouldDeleteSub = false;

let promises = newFilteredCategory.map((itemFilter)=> {
    let arrItemFilter = Object.assign({}, itemFilter);

    if(itemFilter.id == key.id) {

        subfound = false;
        mainFound = true;
        let subIndex = 0;

        arrItemFilter.subCategory.map((subItem, i)=> { 
            let newSubItem = Object.assign({}, subItem);
            subIndex = i
            datafound = false; 
            if(newSubItem.id == subKey.id) {

                subfound = true

                newSubItem.data.forEach((data)=> {
                    if(data.name == dataDetail.name) {
                        datafound = true

                    }
                })

                if(!datafound) {
                    newSubItem.data.push(dataDetail)
                    console.log("push", dataDetail)
                }else {


                    newSubItem.data =  newSubItem.data.filter((f)=> {
                        if(f.name != dataDetail.name) {
                            return f
                        }
                    })

                }

            }


            if(newSubItem.data.length == 0) {
                shouldDeleteSub = true
            }
            console.log("798 arrItemFilter", newSubItem)
            return newSubItem
        })

        console.log("802 arrItemFilter", arrItemFilter)



        if(arrItemFilter.subCategory.length == 0) {
            shouldDeleteMain = true
        }


        console.log("arrItemFilter", arrItemFilter)
        return arrItemFilter
    }

    return arrItemFilter
})

Promise.all(promises).then((newFilteredCategory) => {
    let index =  newFilteredCategory.findIndex(i => i.id === key.id );

    if(shouldDeleteSub) {
        let subIndex = newFilteredCategory[index].subCategory.findIndex(i => i.id === subKey.id );

        newFilteredCategory[index].subCategory.splice(subIndex, 1);
        console.log(newFilteredCategory)



        if(newFilteredCategory[index].subCategory.length == 0) {
            shouldDeleteMain = true
        }


    }

    if(shouldDeleteMain) {
        newFilteredCategory.splice(index, 1);
        console.log("821", newFilteredCategory)
    }

    if(!mainFound) {
        let object = {};
        object.id = key.id;
        object.name = key.name;
        object.subCategory = [];
        object.subCategory.push({
            id: subKey.id,
            name: subKey.name,
            data: [
                {
                    name: dataDetail.name,
                    date: dataDetail.date
                }
            ]
        })
        newFilteredCategory.push(object);
    }

    if(mainFound && !subfound) {

        newFilteredCategory[index].subCategory.push({
                id: subKey.id,
                name: subKey.name,
                data: [
                    {
                        name: dataDetail.name,
                        date: dataDetail.date
                    }
                ]
            })
    }

    console.log("newFilteredCategory", newFilteredCategory)


    this.setState({filteredCategory: newFilteredCategory})

})

如何对promises数组进行promise.all? 我在比较两个数组,但有时返回的结果不正确,我认为这是因为诺言不正确

有人可以帮助我吗?谢谢。

0 个答案:

没有答案