簡體   English   中英

從對象數組中提取 object 元素並將它們組織成數組

[英]Pulling object elements from an array of objects and organizing them into an array

經過幾天的拉扯,我決定尋求幫助:

  • 我有一組對象,每個雜貨店商品一個。 每個 object 都有 3 個屬性:category(商品類別的字符串)、itemName(商品本身的字符串)和 onSale(布爾值是否在銷售):

     var itemData = [ { category: 'fruit', itemName: 'apple', onSale: false }, { category: 'canned', itemName: 'beans', onSale: false }, { category: 'canned', itemName: 'corn', onSale: true }, { category: 'frozen', itemName: 'pizza', onSale: false }, { category: 'fruit', itemName: 'melon', onSale: true }, { category: 'canned', itemName: 'soup', onSale: false }, ];
  • 我需要將其轉換為 object,其屬性為 object 類別。 每個屬性都是一個數組,其中包含與屬性對應的 itemName 中的值:

     { fruit: ['apple', 'melon($)'], canned: ['beans', 'corn($)', 'soup'], frozen: ['pizza'] };
  • 當我嘗試這樣做時,我最終得到方向正確的值,除了它們被重復:

     { fruit: [ 'apple', 'apple', 'melon', 'melon', 'melon' ], canned: [ 'beans', 'beans', 'corn', 'corn', 'corn', 'soup', 'soup', 'soup' ], frozen: [ 'pizza', 'pizza' ] }
  • 我花了幾個小時試圖找出我哪里出錯了,但無濟於事。 這是我的代碼:

     function organizeItems (array){ //create output obj var output = {} //iterate over the array for(var i = 0; i < array.length; i++){ //iterate over the object in the array for(var category in array[i]){ //if value of category isn't in output add the value to output as an empty array if(output[array[i]["category"]] === undefined){ output[array[i]["category"]] = []; //if not --> push the itemName to the category } else{ output[array[i]["category"]].push(array[i]["itemName"]) } } } return output }

如何避免重復並最終在 arrays 中獲得正確的值?

for(var category in array[i]){...

您不必遍歷每個 object 的條目,因為這會將不必要的元素添加到數組中,例如 output 'melon', 'melon', 'melon'


您可以使用Array.prototype.reduce

 const itemData = [ { category: 'fruit', itemName: 'apple', onSale: false }, { category: 'canned', itemName: 'beans', onSale: false }, { category: 'canned', itemName: 'corn', onSale: true }, { category: 'frozen', itemName: 'pizza', onSale: false }, { category: 'fruit', itemName: 'melon', onSale: true }, { category: 'canned', itemName: 'soup', onSale: false }, ]; const object = itemData.reduce((acc,el) => (acc[el.category]??= [], acc[el.category].push(el.itemName + (el.onSale? '($)': '')), acc), {}) console.log(object)

您可以按category分組並調整itemName

 const itemData = [{ category: 'fruit', itemName: 'apple', onSale: false }, { category: 'canned', itemName: 'beans', onSale: false }, { category: 'canned', itemName: 'corn', onSale: true }, { category: 'frozen', itemName: 'pizza', onSale: false }, { category: 'fruit', itemName: 'melon', onSale: true }, { category: 'canned', itemName: 'soup', onSale: false }], result = itemData.reduce((r, { category, itemName, onSale }) => { itemName += onSale? ' ($)': ''; (r[category]??= []).push(itemName); return r; }, {}); console.log(result);
 .as-console-wrapper { max-height: 100%;important: top; 0; }

 var itemData = [ { category: 'fruit', itemName: 'apple', onSale: false }, { category: 'canned', itemName: 'beans', onSale: false }, { category: 'canned', itemName: 'corn', onSale: true }, { category: 'frozen', itemName: 'pizza', onSale: false }, { category: 'fruit', itemName: 'melon', onSale: true }, { category: 'canned', itemName: 'soup', onSale: false }, ]; // // expected result... // { // fruit: ['apple', 'melon($)'], // canned: ['beans', 'corn($)', 'soup'], // frozen: ['pizza'] // }; console.log( itemData.reduce((result, item) => { // destructure `itemData`'s array `item`. const { category, itemName, onSale } = item; // (create and/or) access the `category` specific array. const categoryArray = (result[category]??= []); // push the correct (depending on `onSale` state) form of a category item. categoryArray.push(`${ itemName }${ (onSale === true)? '($)': '' }`); return result; }, {}) );
 .as-console-wrapper { min-height: 100%;important: top; 0; }

暫無
暫無

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

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