0

I am trying to return the array of all the intersected array elements.

I got 2 arrays.

The array from api and the filter condition array.

Array from api is this

let somethingList = [
      {
        id: 'PROD108',
        name: 'Headsweats Mid Cap',
        CustomFields: [
          {
            name: 'Brand',
            value: 'Headsweats',
          },
          {
            name: 'Eco',
            value: 'False',
          },
          {
            name: 'Test',
            value: '0',
          },
        ],
      },
      {
        id: 'PROD109',
        name: 'Performance Liberty City Cycling Cap',
        CustomFields: [
          {
            name: 'Brand',
            value: 'Performance',
          },
          {
            name: 'Eco',
            value: 'False',
          },
          {
            name: 'Test',
            value: '0',
          },
        ],
      },
      {
        id: 'PROD110',
        name: 'Castelli Logo Bandana',
        CustomFields: [
          {
            name: 'Brand',
            value: 'Castelli',
          },
          {
            name: 'Eco',
            value: 'False',
          },
          {
            name: 'Test',
            value: '0',
          },
        ],
      },
      {
        id: 'PROD159',
        name: 'Performance Classic Sleeveless Jersey',
        CustomFields: [
          {
            name: 'Eco',
            value: 'False',
          },
          {
            name: 'Color',
            value: '#4CAF50',
          },
          {
            name: 'Test',
            value: '0',
          },
        ],
      },
      {
        id: 'PROD160',
        name: 'Schwinn Evolution IC Sleeveless Jersey',
        CustomFields: [
          {
            name: 'Brand',
            value: 'Schwinn',
          },
          {
            name: 'Eco',
            value: 'False',
          },
          {
            name: 'Color',
            value: '#2196F3',
          },
          {
            name: 'Test',
            value: '0',
          },
        ],
      },
      {
        id: 'PROD161',
        name: 'Performance Elite Short',
        CustomFields: [
          {
            name: 'Brand',
            value: 'Performance',
          },
          {
            name: 'Eco',
            value: 'False',
          },
          {
            name: 'Color',
            value: '#000000',
          },
          {
            name: 'Test',
            value: '0',
          },
        ],
      },
      {
        id: 'PROD162',
        name: 'Andiamo! Padded Cycling Brief',
        CustomFields: [
          {
            name: 'Eco',
            value: 'False',
          },
          {
            name: 'Color',
            value: '#808080',
          },
          {
            name: 'Test',
            value: '0',
          },
        ],
      },
      {
        id: 'PROD163',
        name: 'Fox Mojave Glove',
        CustomFields: [
          {
            name: 'Brand',
            value: 'Fox',
          },
          {
            name: 'Eco',
            value: 'False',
          },
          {
            name: 'Color',
            value: '#000000',
          },
          {
            name: 'Test',
            value: '0',
          },
        ],
      },
    ];

filter condition array.

 let testingFilter = ['Fox', 'Performance'];

What I want to do is if the customfield value of array from api is intersected with testingFilter value

I want to push them into an array and return that new array.

But the code I written don't return an new array, What should I do to return a new array

let filteredProduct = [];
    filteredProduct = _.filter(somethingList, (product) => {
      if (testingFilter.length === 0) {
        return somethingList;
      } else {
        // Here is the problem
        return _.intersection(testingFilter, _.map(product.CustomFields, 'value'));
      }
    });

Expected Answer array

filteredProduct = [
{
        id: 'PROD109',
        name: 'Performance Liberty City Cycling Cap',
        CustomFields: [
          {
            name: 'Brand',
            value: 'Performance',
          },
          {
            name: 'Eco',
            value: 'False',
          },
          {
            name: 'Test',
            value: '0',
          },
        ],
      },
{
        id: 'PROD161',
        name: 'Performance Elite Short',
        CustomFields: [
          {
            name: 'Brand',
            value: 'Performance',
          },
          {
            name: 'Eco',
            value: 'False',
          },
          {
            name: 'Color',
            value: '#000000',
          },
          {
            name: 'Test',
            value: '0',
          },
        ],
      },
         {
            id: 'PROD163',
            name: 'Fox Mojave Glove',
            CustomFields: [
              {
                name: 'Brand',
                value: 'Fox',
              },
              {
                name: 'Eco',
                value: 'False',
              },
              {
                name: 'Color',
                value: '#000000',
              },
              {
                name: 'Test',
                value: '0',
              },
            ],
          },
]
1
  • Can you show an example of an expected answer ? Commented May 26, 2021 at 9:49

2 Answers 2

1

The following line taken from your code:

_.map(product.CustomFields, 'value')

Here, you get an array of all the values's of the CustomFields, if we check if there is an item from testinFilter present in that array, we can use that as the _filter return statement like so:

let filteredProduct = [];
filteredProduct = _.filter(somethingList, (product) => {
    const allCustomFields = _.map(product.CustomFields, 'value');
    return allCustomFields.some(r => testingFilter.indexOf(r) >= 0);
});

// We could rewrite the same code as a one-liner without the extra const like so:
let filteredProduct = _.filter(somethingList, (product) => _.map(product.CustomFields, 'value').some(r => testingFilter.indexOf(r) >= 0));

Snippet:

let testingFilter = ['Fox', 'Performance'];
let somethingList = [{id: 'PROD108', name: 'Headsweats Mid Cap', CustomFields: [{name: 'Brand', value: 'Headsweats', }, {name: 'Eco', value: 'False', }, {name: 'Test', value: '0', }, ], }, {id: 'PROD109', name: 'Performance Liberty City Cycling Cap', CustomFields: [{name: 'Brand', value: 'Performance', }, {name: 'Eco', value: 'False', }, {name: 'Test', value: '0', }, ], }, {id: 'PROD110', name: 'Castelli Logo Bandana', CustomFields: [{name: 'Brand', value: 'Castelli', }, {name: 'Eco', value: 'False', }, {name: 'Test', value: '0', }, ], }, {id: 'PROD159', name: 'Performance Classic Sleeveless Jersey', CustomFields: [{name: 'Eco', value: 'False', }, {name: 'Color', value: '#4CAF50', }, {name: 'Test', value: '0', }, ], }, {id: 'PROD160', name: 'Schwinn Evolution IC Sleeveless Jersey', CustomFields: [{name: 'Brand', value: 'Schwinn', }, {name: 'Eco', value: 'False', }, {name: 'Color', value: '#2196F3', }, {name: 'Test', value: '0', }, ], }, {id: 'PROD161', name: 'Performance Elite Short', CustomFields: [{name: 'Brand', value: 'Performance', }, {name: 'Eco', value: 'False', }, {name: 'Color', value: '#000000', }, {name: 'Test', value: '0', }, ], }, {id: 'PROD162', name: 'Andiamo! Padded Cycling Brief', CustomFields: [{name: 'Eco', value: 'False', }, {name: 'Color', value: '#808080', }, {name: 'Test', value: '0', }, ], }, {id: 'PROD163', name: 'Fox Mojave Glove', CustomFields: [{name: 'Brand', value: 'Fox', }, {name: 'Eco', value: 'False', }, {name: 'Color', value: '#000000', }, {name: 'Test', value: '0', }, ], }, ];

let filteredProduct = _.filter(somethingList, (product) => {
    const allCustomFields = _.map(product.CustomFields, 'value');
    return allCustomFields.some(r => testingFilter.indexOf(r) >= 0);
});
console.log(filteredProduct);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>

Result:

[
  {
    "id": "PROD109",
    "name": "Performance Liberty City Cycling Cap",
    "CustomFields": [
      {
        "name": "Brand",
        "value": "Performance"
      },
      {
        "name": "Eco",
        "value": "False"
      },
      {
        "name": "Test",
        "value": "0"
      }
    ]
  },
  {
    "id": "PROD161",
    "name": "Performance Elite Short",
    "CustomFields": [
      {
        "name": "Brand",
        "value": "Performance"
      },
      {
        "name": "Eco",
        "value": "False"
      },
      {
        "name": "Color",
        "value": "#000000"
      },
      {
        "name": "Test",
        "value": "0"
      }
    ]
  },
  {
    "id": "PROD163",
    "name": "Fox Mojave Glove",
    "CustomFields": [
      {
        "name": "Brand",
        "value": "Fox"
      },
      {
        "name": "Eco",
        "value": "False"
      },
      {
        "name": "Color",
        "value": "#000000"
      },
      {
        "name": "Test",
        "value": "0"
      }
    ]
  }
]
Sign up to request clarification or add additional context in comments.

Comments

0

You can do:

let somethingList = [ { id: 'PROD108', name: 'Headsweats Mid Cap', CustomFields: [ { name: 'Brand', value: 'Headsweats', }, { name: 'Eco', value: 'False', }, { name: 'Test', value: '0', }, ], }, { id: 'PROD109', name: 'Performance Liberty City Cycling Cap', CustomFields: [ { name: 'Brand', value: 'Performance', }, { name: 'Eco', value: 'False', }, { name: 'Test', value: '0', }, ], }, { id: 'PROD110', name: 'Castelli Logo Bandana', CustomFields: [ { name: 'Brand', value: 'Castelli', }, { name: 'Eco', value: 'False', }, { name: 'Test', value: '0', }, ], }, { id: 'PROD159', name: 'Performance Classic Sleeveless Jersey', CustomFields: [ { name: 'Eco', value: 'False', }, { name: 'Color', value: '#4CAF50', }, { name: 'Test', value: '0', }, ], }, { id: 'PROD160', name: 'Schwinn Evolution IC Sleeveless Jersey', CustomFields: [ { name: 'Brand', value: 'Schwinn', }, { name: 'Eco', value: 'False', }, { name: 'Color', value: '#2196F3', }, { name: 'Test', value: '0', }, ], }, { id: 'PROD161', name: 'Performance Elite Short', CustomFields: [ { name: 'Brand', value: 'Performance', }, { name: 'Eco', value: 'False', }, { name: 'Color', value: '#000000', }, { name: 'Test', value: '0', }, ], }, { id: 'PROD162', name: 'Andiamo! Padded Cycling Brief', CustomFields: [ { name: 'Eco', value: 'False', }, { name: 'Color', value: '#808080', }, { name: 'Test', value: '0', }, ], }, { id: 'PROD163', name: 'Fox Mojave Glove', CustomFields: [ { name: 'Brand', value: 'Fox', }, { name: 'Eco', value: 'False', }, { name: 'Color', value: '#000000', }, { name: 'Test', value: '0', }, ], }, ];
let testingFilter = ['Fox', 'Performance'];

let filteredProducts = somethingList.filter(p =>
    Array.from(p.CustomFields.values()) // iterable to array
        .map(({value}) => value) 
        .some(value => testingFilter.includes(value))) 

console.log(filteredProducts)

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.