1

我想從下面的json過濾相同顏色的對象,並且每個顏色值都包含兩個值(顏色和數值)的組合,但我只是想根據顏色進行過濾。javascript - 按屬性對數組中的元素進行分組

這裏我曾嘗試

var _ = require('underscore-plus'); 
var data = [{ 
"name": "jim", 
    "color": "blue 1", 
    "age": "22" 
}, { 
"name": "Sam", 
    "color": "blue 2", 
    "age": "33" 
}, { 
"name": "eddie", 
    "color": "green 1", 
    "age": "77" 
}, 
{ 
"name": "Dheeraj", 
    "color": "blue 3", 
    "age": "25" 
}, 
{ 
"name": "Suraj", 
    "color": "green 1", 
    "age": "25" 
} 
]; 

var result=_.groupBy(data,"color"); 
console.log(result) 

結果應當是具有相同顏色的對象陣列。

[{ "name": "jim", "color": "blue 1", "age": "22" }, 
{ "name": "Sam", "color": "blue 2", "age": "33" }, 
{ "name": "Dheeraj", "color": "blue 3", "age": "25" }] 

[{ "name": "Suraj", "color": "green 1", "age": "25" }, 
{ "name": "eddie", "color": "green 1", "age": "77" }] 
+0

過濾器?分組?請添加想要的結果。 –

+0

結果應該是具有相同顏色的對象的數組。 [{ 「名」: 「吉姆」, 「顏色」: 「青1」, 「時代」: 「22」 },{ 「名」: 「山姆」, 「顏色」:「藍色2" , 「年齡」: 「33」 },{ 「名稱」: 「Dheeraj」, 「顏色」: 「藍3」, 「年齡」: 「25」 }] 和[{ 「名」: 「蘇拉傑」, 「顏色」: 「綠1」, 「時代」: 「25」 },{ 「名」: 「埃迪」, 「顏色」: 「綠1」, 「age」:「77」 }] –

回答

0

你可以組:

如果你想,而不是篩選,在問題中提到,您可以使用下劃線filter方法。

var data = [{ "name": "jim", "color": "blue 1", "age": "22" }, { "name": "Sam", "color": "blue 2", "age": "33" }, { "name": "eddie", "color": "green 1", "age": "77" }, { "name": "Dheeraj", "color": "blue 3", "age": "25" }, { "name": "Suraj", "color": "green 1", "age": "25" }], 
 
    grouped = {}, 
 
    colors; 
 

 
data.forEach(function (a) { 
 
    var group = a.color.match(/^[a-z]+/i); 
 
    grouped[group] = grouped[group] || []; 
 
    grouped[group].push(a); \t \t \t 
 
}); 
 
colors = Object.keys(grouped); 
 
colors.forEach(function (color) { 
 
    console.log(color, grouped[color]); 
 
});
.as-console-wrapper { max-height: 100% !important; top: 0; }

+0

感謝妮娜它按預期工作,你能不能請解釋一下代碼。 –

+0

基本上它從'color'屬性獲取字母並將其用作對象的關鍵字。那麼它會檢查'grouped'中的屬性是否存在,如果不存在,則爲其分配一個空數組。稍後使用顏色的關鍵點將éterated數組的實際元素推送到「分組」對象。 –

0

您可以使用Jquery.grep()例如

var result = $.grep(data, function(n){ return n.color == "blue 3" })

0

只需使用groupBy功能用下劃線文檔here詳見:

var result = _.groupBy(data, function(datum) { return datum.color; }); 

您需要提供一個要使用的函數,該函數將返回屬性以將元素進行分組,這種情況下爲顏色。通過顏色

var blueOne = _.filter(data, function(datum){ return datum.color == 'blue 1'; }); 
1

您可以將項目使用Array.prototype.reduce

var data = [{ 
 
    "name": "jim", 
 
    "color": "blue 1", 
 
    "age": "22" 
 
}, { 
 
    "name": "Sam", 
 
    "color": "blue 2", 
 
    "age": "33" 
 
}, { 
 
    "name": "eddie", 
 
    "color": "green 1", 
 
    "age": "77" 
 
}, { 
 
    "name": "Dheeraj", 
 
    "color": "blue 3", 
 
    "age": "25" 
 
}, { 
 
    "name": "Suraj", 
 
    "color": "green 1", 
 
    "age": "25" 
 
}]; 
 

 
var result = data.reduce(function(grouped, obj) { 
 
    var key = obj.color.split(' ')[0]; // get the color from the key 
 
    grouped[key] = (grouped[key] || []).concat(obj); // use the existing array or create a new array, add the object to it, and assign it to the grouped object 
 
    
 
    return grouped; // return the grouped object 
 
}, {}); 
 

 
console.log(result);

+0

這似乎是答案的一半。 – Redu

+0

請詳細說明。 –

+0

爲了進一步解釋這個問題,以一種非常規的方式,給出了預期的結果,對他自己的問題發表評論,我想在結果上需要做更多的工作。 – Redu

相關問題