1

I am trying to filter same color objects from the below json and every color value is containing combination of two values(color and numeric value) but I just want to filter on the basis of color.

Here what i have tried

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)

Result should be array of Objects having same color.

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

and

[{ "name": "Suraj", "color": "green 1", "age": "25" },
 { "name": "eddie", "color": "green 1", "age": "77" }]
2
  • filter? grouping? please add the wanted result. Commented Nov 4, 2016 at 20:04
  • Result should be array of Objects having same color. [{ "name": "jim", "color": "blue 1", "age": "22" }, { "name": "Sam", "color": "blue 2", "age": "33" },{ "name": "Dheeraj", "color": "blue 3", "age": "25" } ] and [{ "name": "Suraj", "color": "green 1", "age": "25" },{ "name": "eddie", "color": "green 1", "age": "77" } ] Commented Nov 4, 2016 at 20:10

4 Answers 4

1

You can group the items using 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);

Sign up to request clarification or add additional context in comments.

9 Comments

this seems to be the half way of the answer.
Please elaborate.
OP, in a non conventional way, in order to explain the question further, gave the expected result in a comment to his own question and i guess it takes a little more doing on top of your result.
@Redu thanks. I've used the groupBy definition. If the OP wants, a single array, it's more of a filter. I'll let him decide.
@Redu Sorry i forgot to add expected result in comment, I have added now.
|
0

You could group by the color.

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);			
});
colors = Object.keys(grouped);
colors.forEach(function (color) {
    console.log(color, grouped[color]);
});
.as-console-wrapper { max-height: 100% !important; top: 0; }

2 Comments

Thanks Nina Its working as expected, Could you Kindly explain the code.
basically it gets the letters from the color property and use it as key for an obejct. then it checks if the property in grouped exists and if not, the assign an empty array to it. later push the actual element of the íterated array to the grouped object with the key of the color.
0

you can use Jquery.grep() for example

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

Comments

0

Simply using the groupBy function as detailed by underscore docs here:

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

You need to provide a function to be used, that will return the attribute to group the elements on, which is color in this case.

If you want to filter instead, as is mentioned in the question, you can use the underscore filter method:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.