2013-08-25 38 views
0

我有一個JSON對象如下如何使JSON對象從具有唯一值JSON對象的元素在下劃線JS

[ 
    { 
     "MerchantName": "Fashion and You", 
     "BrandList": " Nike, Fila", 
     "MerchantImage": "Fashion-You-medium.jpeg" 
    }, 
    { 
     "MerchantName": "Fashion and You", 
     "BrandList": " Levis, Fasttrack, Fila", 
     "MerchantImage": "Fashion-You-medium.jpeg" 
    }, 
    { 
     "MerchantName": "ebay", 
     "BrandList": "Nokia,HTC,Samsung", 
     "MerchantImage": "ebay.jpeg" 
    }, 
    { 
     "MerchantName": "amazon", 
     "BrandList": "Apple,Dell,Samsung", 
     "MerchantImage": "amazon.jpeg" 
    }, 
    { 
     "MerchantName": "amazon", 
     "BrandList": " pepe jeans, peter england, red tape", 
     "MerchantImage, Fila": "amazon.jpeg" 
    } 
] 

我需要用獨特的BrandList JSON對象,如下面的下劃線。

[{"Nike"}, {"Fila"},{"Levis"}, {"Fasttrack"},{"Nokia"}, {"HTC"},{"Samsung"}, {"pepe jeans"}, {"peter england"},{"red tape"}] 

我可以如下獲取數據而不是上述格式,品牌必須是唯一的。

brands = [{brand:"Nike",status:false}, {brand:"Fila",status:false}, {brand:"Levis",status:false},{brand:"Fasttrack",status:false}, {brand:"Nokia",status:false},{brand:"HTC",status:false}, {brand:"Samsung",status:false} ] 
+1

'_.pluck()'很容易處理 - 在Underscore文檔中查找 – Bojangles

+0

您的問題真的是關於JSON還是關於在JavaScript中處理數據?另請注意,您所需的結果不是有效的JSON。 –

+0

當我使用_.pluck()即時獲取數據爲[「耐克,菲拉」,「萊維斯,Fasttrack,菲拉」] –

回答

1
var col = [ 
    { 
     "MerchantName": "Fashion and You", 
     "BrandList": " Nike, Fila", 
     "MerchantImage": "Fashion-You-medium.jpeg" 
    }, 
    { 
     "MerchantName": "Fashion and You", 
     "BrandList": " Levis, Fasttrack, Fila", 
     "MerchantImage": "Fashion-You-medium.jpeg" 
    }, 
    { 
     "MerchantName": "ebay", 
     "BrandList": "Nokia,HTC,Samsung", 
     "MerchantImage": "ebay.jpeg" 
    }, 
    { 
     "MerchantName": "amazon", 
     "BrandList": "Apple,Dell,Samsung", 
     "MerchantImage": "amazon.jpeg" 
    }, 
    { 
     "MerchantName": "amazon", 
     "BrandList": " pepe jeans, peter england, red tape", 
     "MerchantImage, Fila": "amazon.jpeg" 
    } 
]; 

var brands = []; 
//get unique brands 
_.each(col, function(i){ 
    brands = _.union(brands,i.BrandList.split(',')); 
}); 

//build output 
brands = _.map(brands, function(brand){ 
    return { brand : brand, status : false}; 
}); 
console.log(brands); 

//if you need json output 
var brandsJson = JSON.stringify(brands); 
console.log(brandsJson); 

的源代碼http://jsfiddle.net/DvnvN/

+0

感謝nikolai,我們可以做到沒有循環? –

+0

我看不出沒有循環的方式。 –

+1

@nikhilreddy:請注意,Nikolai使用一組對象作爲示例。如果以JSON的形式獲取數據,則必須先解析它('JSON.parse'),將代碼應用於結果(一個對象數組),然後使用'JSON.stringify'將其轉換回JSON。 –

0

如前所述你列出的JSON對象是無效的。如果你正在尋找一種方式來填充的只是獨特的品牌名稱的數組,你可以使用一些強調功能 -

var arr = ...; 

function trim(str){ 
    return str.replace(/^\s+|\s+$/g, ""); 
} 

var mapped = _.map(_.pluck(arr, 'BrandList'), function(type){ 
    return _.map(type.split(","), function(brand){ 
    return trim(brand); 
    }); 
}); 

var unique = _.uniq(_.flatten(mapped)); 
//outputs ["Nike", "Fila", "Levis", "Fasttrack", "Nokia", "HTC", "Samsung", "Apple", "Dell", "pepe jeans", "peter england", "red tape"] 

我不知道這是任何容易然後讀取一個簡單的循環,並在過程中創建幾個中間數組,但它完成了工作。