亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我走在正確的軌道上嗎?在 JS 中編寫一個必須計算對象類別中的塊數的函數

我走在正確的軌道上嗎?在 JS 中編寫一個必須計算對象類別中的塊數的函數

桃花長相依 2023-08-24 10:20:36
我是一個初學者,試圖弄清楚這一點,但非常困難!我需要編寫一個函數,它接受一個客戶對象數組并返回一個僅包含購買了 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)
查看完整描述

4 回答

?
慕尼黑的夜晚無繁華

TA貢獻1864條經驗 獲得超6個贊

您當前的地圖代碼有錯誤。我也在示例中修復了它。

要回答您的問題,您需要使用過濾功能來查找您需要的項目。

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)


查看完整回答
反對 回復 2023-08-24
?
白豬掌柜的

TA貢獻1893條經驗 獲得超10個贊

您不想使用地圖,而是想使用過濾器。過濾器只會返回匹配的元素。


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)


查看完整回答
反對 回復 2023-08-24
?
慕妹3242003

TA貢獻1824條經驗 獲得超6個贊

你實際上是在尋找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);


查看完整回答
反對 回復 2023-08-24
?
慕田峪9158850

TA貢獻1794條經驗 獲得超7個贊

let over5Items = Customers.map((element) => {
    return element.itemsPurchased.length > 5;
})


查看完整回答
反對 回復 2023-08-24
  • 4 回答
  • 0 關注
  • 264 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號