Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 比较ReactJS中不同数组的JSON值_Arrays_Json_Reactjs_For Loop_If Statement - Fatal编程技术网

Arrays 比较ReactJS中不同数组的JSON值

Arrays 比较ReactJS中不同数组的JSON值,arrays,json,reactjs,for-loop,if-statement,Arrays,Json,Reactjs,For Loop,If Statement,我正试图在react中构建一个系统,该系统将使我能够呈现JSON文件中的产品列表,并基于JSON中的信息比较不同的数组,并根据JSON中称为捆绑包的不同包中包含的产品分配最小产品数量。用户将能够选择一个捆绑包并查看包含哪些产品,这需要是动态的,因为JSON可能会随着时间的推移而改变 JSON信息如下所示: { "data": { "adverts": [], "bundles": [{ "id": "1",

我正试图在react中构建一个系统,该系统将使我能够呈现JSON文件中的产品列表,并基于JSON中的信息比较不同的数组,并根据JSON中称为捆绑包的不同包中包含的产品分配最小产品数量。用户将能够选择一个捆绑包并查看包含哪些产品,这需要是动态的,因为JSON可能会随着时间的推移而改变

JSON信息如下所示:

{
    "data": {
        "adverts": [],
        "bundles": [{
            "id": "1",
            "name": "Bronze Bundle",
            "price": {
                "installation": "99.99",
                "recurring": "23.99"
            },
            "products": ["1", "2", "3", "4", "9", "10", "15", "15"]
        }, {
            "id": "2",
            "name": "Silver Bundle",
            "price": {
                "installation": "99.99",
                "recurring": "23.99"
            },
            "products": ["1", "2", "2", "2", "2", "4", "9", "10", "15", "15"]
        }, {
            "id": "3",
            "name": "Gold Bundle",
            "price": {
                "installation": "99.99",
                "recurring": "25.99"
            },
            "products": ["1", "2", "4", "5", "9", "10", "15", "15"]
        }, {
            "id": "4",
            "name": "Build Your Own Bundle",
            "price": {
                "installation": "49.99",
                "recurring": "9.99"
            },
            "products": ["1", "10"]
        }],
        "products": [{
            "id": "1",
            "name": "Product 1",
            "price": {
                "upfront": null,
                "installation": "0.00",
                "recurring": "0.00"
            }
        },  {
            "id": "3",
            "name": "Product 3",
            "price": {
                "upfront": "132.00",
                "installation": "9.60",
                "recurring": "2.75"
            }
        }, {
            "id": "4",
            "name": "Product 4",
            "price": {
                "upfront": "60.00",
                "installation": "9.60",
                "recurring": "1.25"
            }
        }, {
            "id": "2",
            "name": "Product 2",
            "price": {
                "upfront": "60.00",
                "installation": "9.60",
                "recurring": "1.25"
            }
        },{
            "id": "5",
            "name": "Product 5",
            "price": {
                "upfront": "228.00",
                "installation": "9.60",
                "recurring": "4.75"
            }
        }, {
            "id": "6",
            "name": "Product 6",
            "price": {
                "upfront": "96.00",
                "installation": "9.60",
                "recurring": "2.00"
            }

        }]
    }
}
function foo(arr) {
        var a = [], b = [], prev;

        arr.sort();
        for ( var i = 0; i < arr.length; i++ ) {
            if ( arr[i] !== prev ) {
                a.push(arr[i]);
                b.push(1);
            } else {
                b[b.length-1]++;
            }
            prev = arr[i];
        }
        return {pincluded: [a],
                qincluded: [b]
                };

    }   
本质上,我希望将Bundle数组与Products数组进行比较,并根据包含的产品数量为产品分配一个最小值

我使用以下方法将Json导入react:

import productdata from "./catalog.json";
我已将捆绑包中的信息传递到两个数组中,一个显示包含的产品列表,另一个显示产品计数。其代码如下:

{
    "data": {
        "adverts": [],
        "bundles": [{
            "id": "1",
            "name": "Bronze Bundle",
            "price": {
                "installation": "99.99",
                "recurring": "23.99"
            },
            "products": ["1", "2", "3", "4", "9", "10", "15", "15"]
        }, {
            "id": "2",
            "name": "Silver Bundle",
            "price": {
                "installation": "99.99",
                "recurring": "23.99"
            },
            "products": ["1", "2", "2", "2", "2", "4", "9", "10", "15", "15"]
        }, {
            "id": "3",
            "name": "Gold Bundle",
            "price": {
                "installation": "99.99",
                "recurring": "25.99"
            },
            "products": ["1", "2", "4", "5", "9", "10", "15", "15"]
        }, {
            "id": "4",
            "name": "Build Your Own Bundle",
            "price": {
                "installation": "49.99",
                "recurring": "9.99"
            },
            "products": ["1", "10"]
        }],
        "products": [{
            "id": "1",
            "name": "Product 1",
            "price": {
                "upfront": null,
                "installation": "0.00",
                "recurring": "0.00"
            }
        },  {
            "id": "3",
            "name": "Product 3",
            "price": {
                "upfront": "132.00",
                "installation": "9.60",
                "recurring": "2.75"
            }
        }, {
            "id": "4",
            "name": "Product 4",
            "price": {
                "upfront": "60.00",
                "installation": "9.60",
                "recurring": "1.25"
            }
        }, {
            "id": "2",
            "name": "Product 2",
            "price": {
                "upfront": "60.00",
                "installation": "9.60",
                "recurring": "1.25"
            }
        },{
            "id": "5",
            "name": "Product 5",
            "price": {
                "upfront": "228.00",
                "installation": "9.60",
                "recurring": "4.75"
            }
        }, {
            "id": "6",
            "name": "Product 6",
            "price": {
                "upfront": "96.00",
                "installation": "9.60",
                "recurring": "2.00"
            }

        }]
    }
}
function foo(arr) {
        var a = [], b = [], prev;

        arr.sort();
        for ( var i = 0; i < arr.length; i++ ) {
            if ( arr[i] !== prev ) {
                a.push(arr[i]);
                b.push(1);
            } else {
                b[b.length-1]++;
            }
            prev = arr[i];
        }
        return {pincluded: [a],
                qincluded: [b]
                };

    }   
我当前的代码是用来比较数组并为相关产品分配最小值的,这发生在一个单独的react组件中,因此它们是使用props pincluded=passedProducts和qincluded=passedQuantity传递的:

我可以在控制台日志中呈现产品列表,但我需要能够在react中查看它,并根据捆绑包中包含的数量分别为每一行产品分配多个产品


如果有人能就此提供一些建议,我将不胜感激。

这是一个常见的用例,您需要进行一些计算或数据处理,以便能够将其呈现给用户

我经常这样做:

render() {
  const result = processData(productdata.data.products, 
  this.props.passedProducts, this.props.passedQuantity)
  if (!result)
    return (<div>Something went wrong with processing</div>)
  return (... )
}

理想情况下,该函数是一个纯函数,只对输入进行操作,并返回一个对象,而不引用props。这使得它很容易测试。

谢谢,这让我走了很多路