Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

195
Views
Filter a collection for everything except the values in an array (exclude the values in the array)

Trying out the lodash _.filter to return an array of values that excludes the values in an array. Say I have a collection of objects:

let col = {
    data: {
        name: "data1",
        type: "neighbor"
   },
},
{
    data: {
        name: "data2",
        type: "subordinate"
    }
},
{
    data: {
        name: "data3",
        type: "affiliate"
    }
}

let values = ["neighbor", "affiliate"];

I know I can do something like:

const elementsToKeep = _.filter(col, (v) => _.includes(values, v.data("type")));

which would return "neighbor" and "affiliate" but I want to return "subordinate" instead. I know this should be rather simple but I'm having a hard time figuring it out without writing more extensive logic.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Since the data is not structured properly as @BeerusDev mentioned, let me add the code as the answer and try to explain it.

const col = [{
    data: {
      name: "data1",
      type: "neighbor"
    },

  },
  {
    data: {
      name: 'data2',
      type: 'subordinate'
    }
  },
  {
    data: {
      name: "data3",
      type: "affiliate"
    }
  }
];

let values = ["neighbor", "affiliate"];

const elementsToKeep = _.filter(col, (v) => !_.includes(values, v.data["type"]));

console.log(elementsToKeep)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>

Since the result of the _.includes is a boolean value, to get the data which is not inside the array that u have, we need to use !_.includes(...)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!