Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/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
Javascript 使用Axios filtering response.data的API调用_Javascript_Arrays_Json_Api_Axios - Fatal编程技术网

Javascript 使用Axios filtering response.data的API调用

Javascript 使用Axios filtering response.data的API调用,javascript,arrays,json,api,axios,Javascript,Arrays,Json,Api,Axios,我想根据产品日期获得最新的8种产品。我可以获取整个产品阵列并对其进行过滤,如下图所示: const newestProducts= []; axios.get("http://localhost:3003/products").then(response => { let products = response.data.sort(function(a, b) { return new Date(b.date) -

我想根据产品日期获得最新的8种产品。我可以获取整个产品阵列并对其进行过滤,如下图所示:

    const newestProducts= [];
    axios.get("http://localhost:3003/products").then(response => {
        let products = response.data.sort(function(a, b) {
            return new Date(b.date) - new Date(a.date);
        });

        newstProducts = product.slice(0,7)
    });
但是如果我有上千种产品,而且我只需要买最新的8种产品,那就太糟糕了

我想在通话中添加
?\u limit=8

axios.get("http://localhost:3003/products?_limit=8").then{...}
但是,正如你所知,这也不能正常工作,因为它让我获得了阵列中唯一的前8名产品

在我从服务器获取产品之前,是否有任何方法对其进行过滤,或者我必须将其全部存储在var中,然后对其进行过滤

Json文件

      "categories": [ 
            {
            "id": 9,
            "category": "bathroom",
            "date": "2020/8/3",
            "name": "ullam basin mixer",
            "price": 160,
            "img_1": "rim_mixer_01.jpg",
            "img_2": "rim_mixer_02.jpg",
            "rating": 4.5,
            "description": "MARMO is a complete series made of 72 models, with different shapes and sizes for different functions, that keeps uncompromised its elegant beauty. This is a Demo Online Store."
        },
        {
            "id": 10,
            "category": "bathroom",
            "date": "2020/8/19",
            "name": "gravida bathtub",
            "price": 2100,
            "img_1": "inbani_bathtub_01.jpg",
            "img_2": "inbani_bathtub_02.jpg",
            "rating": 4,
            "description": "A young company with a wealth of experience. created in 2004, inbani has evolved into a leader in innovation thanks to a conviction to create products which truly benefit the well-being of the customer.  This is a Demo Online Store. No orders shall be fulfilled."
        },
        {
            "id": 11,
            "category": "bathroom",
            "date": "2020/9/9",
            "name": "vulputate mixer",
            "price": 300,
            "img_1": "marmo_mixer_01.jpg",
            "img_2": "marmo_mixer_02.jpg",
            "description": "MARMO is a complete series made of 72 models, with different shapes and sizes for different functions, that keeps uncompromised its elegant beauty. This is a Demo Online Store."
        },
        {
            "id": 12,
            "category": "bathroom",
            "date": "2018/7/17",
            "name": "aliquam veneatis bathtub",
            "price": 2580,
            "img_1": "sa_oche_01.jpg",
            "img_2": "sa_oche_02.jpg",
            "description": "Its oval, elliptical design with the incongruent walls invokes an avant-garde atmosphere in the bathroom."
        },
        {
            "id": 13,
            "category": "kitchen",
            "date": "2020/3/13",
            "name": "quisque teapot",
            "price": 240,
            "img_1": "theo_teapot_01.jpg",
            "img_2": "theo_teapot_02.jpg",
            "description": "Theo Teapot is a Scandinavian-Japanese teapot made from stoneware and bamboo, designed by Unit 10 Design for Stelton.  This is a Demo Online Store. No orders shall be fulfilled."
        },
        {
            "id": 14,
            "category": "kitchen",
            "date": "2020/2/12",
            "name": "creamic teapot",
            "price": 60,
            "img_1": "cer_teapot_01.jpg",
            "img_2": "cer_teapot_02.jpg",
            "rating": 3.9,
            "description": "Matte ceramic tea pot comes with integrated and removable metal tea infuser. Capacity 700 ml (2.96 cups). Dishwasher safe. This ceramic teapot has a white matte finish, coupled with a square shape and curved lines."
        },
        {
            "id": 15,
            "category": "kitchen",
            "date": "2019/2/1",
            "name": "bottle grinders",
            "price": 30,
            "img_1": "bottle_gringer_01.jpg",
            "img_2": "bottle_gringer_02.jpg",
            "rating": 4.8,
            "description": "Bottle Grinders is a minimal, timeless salt and pepper mill set designed by Norm.Architects for Menu. Steering away from the predictable grinder, the Norm Bottle Grinder is not what you expect to see in a salt and pepper grinder. The form, shaped more like a bottle, cleverly tricks the user to encourage a more playful and experimental interaction with the product. This is a Demo Online Store. No orders shall be fulfilled. Purchase this product"
        },
        {
            "id": 16,
            "category": "lighting",
            "date": "2020/1/3",
            "name": "commodo blown lamp",
            "price": 275,
            "img_1": "tradition_blown_01.jpg",
            "img_2": "tradition_blown_02.jpg",
            "description": "Blown lamp SW3 & SW4 by &tradition is a mouth-blown pendant lamp with a quilted pattern, it comes in a translucent variant with a silver lustre or in a opal white version. Blown lamp is fitted with a powder-coated metal suspension. This is a Demo Online Store. No orders shall be fulfilled."
        },
        {
            "id": 17,
            "category": "lighting",
            "date": "2020/6/9",
            "name": "spot table",
            "price": 100,
            "img_1": "spot_lamp_01.jpg",
            "img_2": "spot_lamp_02.jpg",
            "rating": 3.4,
            "description": "Set the stage. The Spot lamp is a lively, versatile addition to your room. This is a Demo Online Store. No orders shall be fulfilled."
        }
    ]

问题不在API调用中。。。为了提高性能,您必须在后端折射数据库请求以仅获取最后8个产品。 注:
通过在后端的控制器(路由)中发送到数据库的查询来实现这一点

您可以控制数据吗?如果您在源代码中按日期对它们进行排序,那么limit=8的请求应该会起作用。是的,我可以控制数据。并且?_limit=8有效,但它给了我从0索引到7的产品,并对其进行排序。你知道我的意思吗?它不会对整个数据进行排序,它只从0索引到7索引进行排序。如果总数据中只有8条数据要从服务器传输,则必须在服务器端执行数据排序。好的,谢谢大家。