1
I는 아래 JSON마다 색상 값은 두 값 (색상 및 수치)하지만 단지 색에 기초하여 필터링 할 조합을 포함한다에서 동색 개체를 필터링하는 것을 시도하고

. 나 동일한 색을 갖는 개체의 배열이자바 스크립트 - 속성으로 배열 그룹화 요소

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 " }] 및 [{ "이름": "SURAJ" "색상": "녹색 1", "나이": "25" }, { "이름": "에디" "색상": "녹색 1" "나이": "77" }] –

답변

0

당신은 할 수 그룹 : 질문에서 언급 한 바와 같이

대신 필터링하려면

는, 당신은 밑줄 filter 방법을 사용할 수 있습니다. 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" }], 
 
    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

감사합니다. Nina 예상대로 작동합니다. 코드를 설명해 주시겠습니까? –

+0

기본적으로'color' 속성에서 문자를 가져 와서 객체의 키로 사용합니다. 'grouped'의 프로퍼티가 존재하는지 검사하고, 존재하지 않으면 빈 배열을 할당합니다. 나중에 배열의 실제 요소를 색상의 키가있는'grouped' 객체로 푸시합니다. –

0

당신은 예를 들어 Jquery.grep()을 사용할 수 있습니다 var result = $.grep(data, function(n){ return n.color == "blue 3" })

0

밑줄 문서 here에 의해 설명 된대로 단순히 groupBy 기능을 사용하여 :

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

당신은이 경우 색상 인에 그룹에 요소를 속성을 반환합니다 사용할 수있는 기능을 제공해야합니다. 색상에 의해

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

그룹화 할 수있는 항목 :

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

OP는 비 전통적 방식으로 질문을 더 설명하기 위해 자신의 질문에 대한 의견에 예상되는 결과를주었습니다. 결과에 대해 조금 더 노력해야합니다. – Redu

관련 문제