我走在正确的轨道上吗?在 JS 中编写一个必须计算对象类别中的块数的函数

我是一个初学者,试图弄清楚这一点,但非常困难!我需要编写一个函数,它接受一个客户对象数组并返回一个仅包含购买了 5 件以上商品的客户的新数组。我认为可能有更简单的方法来做到这一点,但我想不出来出去!我创建了对象并尝试使用 .map 来评估数组的一部分并返回一个数组。此时我已经让它返回一个数组,但这是我能做的最好的事情。有人可以帮忙吗?我尝试添加 TIA!


function Customer (name, itemsPurchased, numberOfItems) {

        this.name = name;

        this.itemsPurchased = itemsPurchased;

    };

    

    

    var Customers = [

        new Customer("Tim", ["milk", "Coke", "butter", "chips"]),

        new Customer("Sam", ["flour", "sugar", "vanilla", "butter", "chocolate chips", "brown sugar"]),

        new Customer("Sally", ["turkey", "stuffing", "gravy"]),

    ]

    

    let over5Items = Customers.map(function(element) {

        return element.length

    })

    

    console.log (over5Items)


桃花长相依
浏览 178回答 4
4回答

慕尼黑的夜晚无繁华

您当前的地图代码有错误。我也在示例中修复了它。要回答您的问题,您需要使用过滤功能来查找您需要的项目。function Customer (name, itemsPurchased, numberOfItems) {        this.name = name;        this.itemsPurchased = itemsPurchased;    };            var Customers = [        new Customer("Tim", ["milk", "Coke", "butter", "chips"]),        new Customer("Sam", ["flour", "sugar", "vanilla", "butter", "chocolate chips", "brown sugar"]),        new Customer("Sally", ["turkey", "stuffing", "gravy"]),    ]        // how to extract all purchases with .map    const allPurchases = Customers.map(function(element) {        return element.itemsPurchased.length // firstly lenght has to be looking at your array    })            // how to filter to all over 5 purchases    const over5Items = Customers.filter(customer => customer.itemsPurchased.length > 5);        console.log (allPurchases)    console.log (over5Items)

白猪掌柜的

您不想使用地图,而是想使用过滤器。过滤器只会返回匹配的元素。function Customer (name, itemsPurchased, numberOfItems) {        this.name = name;        this.itemsPurchased = itemsPurchased;    };            var Customers = [        new Customer("Tim", ["milk", "Coke", "butter", "chips"]),        new Customer("Sam", ["flour", "sugar", "vanilla", "butter", "chocolate chips", "brown sugar"]),        new Customer("Sally", ["turkey", "stuffing", "gravy"]),    ]        const over5Items = Customers.filter(element =>element.itemsPurchased.length > 5);        console.log (over5Items)

慕妹3242003

你实际上是在寻找Array.filter()。Array.map()返回一个新数组,其元素数量与输入相同,其中每个元素都是给定函数的结果。Array.filter()返回一个新数组,其中每个输入元素都通过给定函数中的测试。function Customer (name, itemsPurchased, numberOfItems) {    this.name = name;    this.itemsPurchased = itemsPurchased;};        var Customers = [    new Customer("Tim", ["milk", "Coke", "butter", "chips"]),    new Customer("Sam", ["flour", "sugar", "vanilla", "butter", "chocolate chips", "brown sugar"]),    new Customer("Sally", ["turkey", "stuffing", "gravy"]),];    let over5Items = Customers.filter(function(element) {    return element.itemsPurchased.length >= 5;});    console.log(over5Items);

慕田峪9158850

let over5Items = Customers.map((element) => {     return element.itemsPurchased.length > 5; })
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript