第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

我走在正確的軌道上嗎?在 JS 中編寫(xiě)一個(gè)必須計(jì)算對(duì)象類別中的塊數(shù)的函數(shù)

我走在正確的軌道上嗎?在 JS 中編寫(xiě)一個(gè)必須計(jì)算對(duì)象類別中的塊數(shù)的函數(shù)

桃花長(zhǎng)相依 2023-08-24 10:20:36
我是一個(gè)初學(xué)者,試圖弄清楚這一點(diǎn),但非常困難!我需要編寫(xiě)一個(gè)函數(shù),它接受一個(gè)客戶對(duì)象數(shù)組并返回一個(gè)僅包含購(gòu)買(mǎi)了 5 件以上商品的客戶的新數(shù)組。我認(rèn)為可能有更簡(jiǎn)單的方法來(lái)做到這一點(diǎn),但我想不出來(lái)出去!我創(chuàng)建了對(duì)象并嘗試使用 .map 來(lái)評(píng)估數(shù)組的一部分并返回一個(gè)數(shù)組。此時(shí)我已經(jīng)讓它返回一個(gè)數(shù)組,但這是我能做的最好的事情。有人可以幫忙嗎?我嘗試添加 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 回答

?
慕尼黑的夜晚無(wú)繁華

TA貢獻(xiàn)1864條經(jīng)驗(yàn) 獲得超6個(gè)贊

您當(dāng)前的地圖代碼有錯(cuò)誤。我也在示例中修復(fù)了它。

要回答您的問(wèn)題,您需要使用過(guò)濾功能來(lái)查找您需要的項(xiàng)目。

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)


查看完整回答
反對(duì) 回復(fù) 2023-08-24
?
白豬掌柜的

TA貢獻(xiàn)1893條經(jīng)驗(yàn) 獲得超10個(gè)贊

您不想使用地圖,而是想使用過(guò)濾器。過(guò)濾器只會(huì)返回匹配的元素。


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)


查看完整回答
反對(duì) 回復(fù) 2023-08-24
?
慕妹3242003

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超6個(gè)贊

你實(shí)際上是在尋找Array.filter()。

Array.map()返回一個(gè)新數(shù)組,其元素?cái)?shù)量與輸入相同,其中每個(gè)元素都是給定函數(shù)的結(jié)果。

Array.filter()返回一個(gè)新數(shù)組,其中每個(gè)輸入元素都通過(guò)給定函數(shù)中的測(cè)試。

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);


查看完整回答
反對(duì) 回復(fù) 2023-08-24
?
慕田峪9158850

TA貢獻(xiàn)1794條經(jīng)驗(yàn) 獲得超8個(gè)贊

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


查看完整回答
反對(duì) 回復(fù) 2023-08-24
  • 4 回答
  • 0 關(guān)注
  • 290 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)