const cardData = [ { suit: "heart", value: 7, }, { suit: "club", value: 8, }, { suit: "club", value: 2, }, { suit: "diamond", value: 2, }, { suit: "diamond", value: 5, }, { suit: "club", value: 10, }, ]; // ================================================= 4 ============================================== // const Obj2 = {}; function getSuitValues(cardData) { for (const card of cardData) { Obj2[card.suit] = Obj2[card.suit] ? [...Obj2[card.suit], card.value] : [card.value]; } return Obj2; } getSuitValues(cardData); /** * 4. write a function which returns an object with the suits as its key and its available values as value * ans => { heart:[7], club:[8,2,10], diamond:[2,5]} * */
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter