Ckappo
Ckappo

Reputation: 597

lodash filter in array

in lodash there is a possibility to filter within an array which is in an object?

I have an object that has an array in it. It looks like this

{
  "id": "1",
  "name": "Test 1",
  "tag": ["blue","red", "yellow"]
},
{
  "id": "2",
  "name": "Test 2",
  "tag": ["red", "yellow"]
},
{
  "id": "3",
  "name": "Test 3",
  "tag": ["green"]
}

What I want to do now. If the tag is Red he should output the object with the ids: 1 and 2. Tag = Green only the object with the id: 3. And so on.

I have now tried to solve this with the lodash filter.

 const filteredColors = _.filter(colors, function(c) {
  return _.includes(['Test 1', 'Test 2'], c.name);
});
// returns Objects with 2 Entrys = Correct

I can filter normal values, but how can I find the value in the array?

Upvotes: 0

Views: 3640

Answers (5)

Akrion
Akrion

Reputation: 18525

This in lodash is somewhat longer than with ES6.

const data = [{ "id": "1", "name": "Test 1", "tag": ["blue","red", "yellow"] }, { "id": "2", "name": "Test 2", "tag": ["red", "yellow"] }, { "id": "3", "name": "Test 3", "tag": ["green"] }]

// lodash
const lodash = c => _.filter(data, x => _.includes(x.tag,c))

// ES6
const es6 = c => data.filter(x => x.tag.includes(c))

console.log(lodash('green'))
console.log(es6('green'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>

The idea in both is to simply use _.filter / Array.filter and then _.includes / Array.includes

Upvotes: 0

Mamun
Mamun

Reputation: 68933

You can use Array.prototype.filter():

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

and Array.prototype.map():

The map() method creates a new array with the results of calling a provided function on every element in the calling array.

const colors = [{
  "id": "1",
  "name": "Test 1",
  "tag": ["blue","red", "yellow"]
},
{
  "id": "2",
  "name": "Test 2",
  "tag": ["red", "yellow"]
},
{
  "id": "3",
  "name": "Test 3",
  "tag": ["green"]
}]

function filteredColors(colorsArr, c){
  return colorsArr.filter(i => i.tag.includes(c)).map(i => ({id: i.id}));
}
console.log(filteredColors(colors, 'red'));
console.log(filteredColors(colors, 'green'));

Upvotes: 0

Biplab Malakar
Biplab Malakar

Reputation: 788

Convert your array to String, and then you can check string is contain your colour or not. Like.

 const items =  [
      {
         "id": "1",
         "name": "Test 1",
         "tag": ["blue","red", "yellow"]
      },
      {
         "id": "2",
         "name": "Test 2",
         "tag": ["red", "yellow"]
      },
      {
         "id": "3",
         "name": "Test 3",
         "tag": ["green"]
 }]

 function findColorId(color){
     return items.filter((d)=> {
         if(String(d.tag).includes(color)){
               return d;
         }
     });
 }

 findColorId('red');

Upvotes: 0

Ckappo
Ckappo

Reputation: 597

I have solved it with:

 let filter = _.filter(
  colors,
  _.flow(
    _.property('tag'),
    _.partial(_.intersection, ['red', 'green']),
    _.size,
  ),
);

Upvotes: 2

CertainPerformance
CertainPerformance

Reputation: 371193

There's no need for lodash, just check if the tag array includes what you're looking for:

const arr = [{
  "id": "1",
  "name": "Test 1",
  "tag": ["blue","red", "yellow"]
},
{
  "id": "2",
  "name": "Test 2",
  "tag": ["red", "yellow"]
},
{
  "id": "3",
  "name": "Test 3",
  "tag": ["green"]
}];
console.log(
  arr.filter(({ tag }) => tag.includes('red'))
);

Upvotes: 2

Related Questions