簡體   English   中英

在 .map() 方法中檢查匹配的字符串

[英]Checking for matching strings in a .map() method

我試圖從一個非常有限的 API 中將相關產品組合在一起,基本上這些相關產品只是顏色的變體。 有一定的命名約定,其中變體/顏色將在標題字符串中寫入T-Shirt [Red]T-Shirt [White] ,因此我將能夠解析標題字符串並查找相等的字符串在任何[字符之前。 考慮到這一點,我將需要使用.split()方法,但是,我不知道如何處理比較方面,也許.map()方法在這里是錯誤的選擇。 任何幫助或指示將不勝感激。

 const clothing = [ { "id": "Z2-Black-TeeID-02", "availableForSale": true, "createdAt": "2019-03-20T17:12:43Z", "title": "T-Shirt [Black]" }, { "id": "Z2-White-TeeID-02", "availableForSale": true, "createdAt": "2019-03-20T17:12:43Z", "title": "T-Shirt [White]" }, { "id": "Z2-TrousersID-02", "availableForSale": true, "createdAt": "2019-03-20T17:12:43Z", "title": "Trousers" }, { "id": "Z2-SocksID-02", "availableForSale": true, "createdAt": "2019-03-20T17:12:43Z", "title": "Socks" }, { "id": "Z2-Red-TeeID-02", "availableForSale": true, "createdAt": "2019-03-20T17:12:43Z", "title": "T-Shirt [Red]" }, ] const productSiblings = clothing.map( (product, index) => ({ id: product.id, title: product.title, index, siblings: [] // Ideally this would contain the IDs of every item in the // array that had a matching string before any '[' character }) ) console.log(productSiblings) // Current Output const currentOutput = [ { "id": "Z2-Black-TeeID-02", "title": "T-Shirt [Black]", "index": 0, "siblings": [] }, { "id": "Z2-White-TeeID-02", "title": "T-Shirt [White]", "index": 1, "siblings": [] }, { "id": "Z2-TrousersID-02", "title": "Trousers", "index": 2, "siblings": [] }, { "id": "Z2-SocksID-02", "title": "Socks", "index": 3, "siblings": [] }, { "id": "Z2-Red-TeeID-02", "title": "T-Shirt [Red]", "index": 4, "siblings": [] } ] // Desired Output const desiredOutput = [ { "id": "Z2-Black-TeeID-02", "title": "T-Shirt [Black]", "index": 0, "siblings": ["Z2-White-TeeID-02", "Z2-Red-TeeID-02"] }, { "id": "Z2-White-TeeID-02", "title": "T-Shirt [White]", "index": 1, "siblings": ["Z2-Black-TeeID-02", "Z2-Red-TeeID-02"] }, { "id": "Z2-TrousersID-02", "title": "Trousers", "index": 2, "siblings": [] }, { "id": "Z2-SocksID-02", "title": "Socks", "index": 3, "siblings": [] }, { "id": "Z2-Red-TeeID-02", "title": "T-Shirt [Red]", "index": 4, "siblings": ["Z2-Black-TeeID-02", "Z2-White-TeeID-02"] } ]

這是一個使用mapfilter的解決方案。 重要的是,我們使用toLowerCasetrim來確保我們的比較不會太嚴格。 此外,我們確保我們不會說一個項目是它自己的兄弟姐妹。

 const clothing = [ { "id": "Z2-Black-TeeID-02", "availableForSale": true, "createdAt": "2019-03-20T17:12:43Z", "title": "T-Shirt [Black]" }, { "id": "Z2-White-TeeID-02", "availableForSale": true, "createdAt": "2019-03-20T17:12:43Z", "title": "T-Shirt [White]" }, { "id": "Z2-TrousersID-02", "availableForSale": true, "createdAt": "2019-03-20T17:12:43Z", "title": "Trousers" }, { "id": "Z2-SocksID-02", "availableForSale": true, "createdAt": "2019-03-20T17:12:43Z", "title": "Socks" }, { "id": "Z2-Red-TeeID-02", "availableForSale": true, "createdAt": "2019-03-20T17:12:43Z", "title": "T-Shirt [Red]" }, ] const similarItems = (item1, item2) => item1.title.toLowerCase().split("[")[0].trim() === item2.title.toLowerCase().split("[")[0].trim(); const related = clothing.map(item => ({ ...item, siblings: clothing .filter(el => el !== item && similarItems(el, item)) .map(el => el.id) })) console.log(related);

getSiblings正在遍歷clothing並返回一系列兄弟姐妹

 const clothing = [ { "id": "Z2-Black-TeeID-02", "availableForSale": true, "createdAt": "2019-03-20T17:12:43Z", "title": "T-Shirt [Black]" }, { "id": "Z2-White-TeeID-02", "availableForSale": true, "createdAt": "2019-03-20T17:12:43Z", "title": "T-Shirt [White]" }, { "id": "Z2-TrousersID-02", "availableForSale": true, "createdAt": "2019-03-20T17:12:43Z", "title": "Trousers" }, { "id": "Z2-SocksID-02", "availableForSale": true, "createdAt": "2019-03-20T17:12:43Z", "title": "Socks" }, { "id": "Z2-Red-TeeID-02", "availableForSale": true, "createdAt": "2019-03-20T17:12:43Z", "title": "T-Shirt [Red]" }, ] const getSiblings = (title) => { const results = [] clothing.forEach(product => { if (product.title !== title && product.title.includes(title.split('[')[0])) { results.push(product.id); } }) return results; } const productSiblings = clothing.map( (product, index) => ({ id: product.id, title: product.title, index, siblings: getSiblings(product.title) }) ); console.log(productSiblings)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM