Filter one array using values from another array using linqjs

801 Views Asked by At

The first one is an array of objects:

let objectArray = [{
    FullName: "Person1",
    PersonId: "id1"
  },
  {
    FullName: "Person2",
    PersonId: "id2"
  },
  {
    FullName: "Person3",
    PersonId: "id3"
  },
  {
    FullName: "Person4",
    PersonId: "id4"
  }
];

The second one is an array of strings containing some ids.

let idsArray= ["id1", "id2", "id3"];

I need to delete the objects of the first array whose id are contained in the second array.

Expected result:

firstArray = [{
  FullName: "Person4",
  PersonId: "id4"
}];

Exploring Linqjs documentation I discovered that the Except() method allows me to remove elements from the first array using the second one as the "filter".

In order to use this method, I need to create a new array from objectArray that contains only the elements whose ids are contained on idsArray to use it as a parameter.

Example:

let filteredArray = Enumerable.From(objectArray).Except(theNewArray).ToArray();

To create this new array I can use the method Where() from Linqjs.

My problem starts here because I don't know how to create this new array considering that I have an array of ids to filter.

4

There are 4 best solutions below

1
Tom O. On BEST ANSWER

You can use Array.prototype.filter method in conjunction with indexOf to test if the PersonId property is found in the array of IDs to exclude - if it's not, add it to the new filteredArray. See below for example:

let objects = [{
    FullName: "Person1",
    PersonId: "id1"
  },
  {
    FullName: "Person2",
    PersonId: "id2"
  },
  {
    FullName: "Person3",
    PersonId: "id3"
  },
  {
    FullName: "Person4",
    PersonId: "id4"
  }
];

let toDelete = ["id1", "id2", "id3"];

//just use Array.prototype.filter method to remove unwanted
var filteredObjects = objects.filter(function(element) {
  return toDelete.indexOf(element.PersonId) === -1;
});

console.log(filteredObjects);

This is achieved using vanilla JavaScript. I would advise you remove linqjs from your project's codebase if this is the only thing you're using it for.

0
Eddie On

You can use filter() to filter the array. Use new Set() to create a set object. This will make easier to check if the PersonId exists. No need to loop every filter()

let objectArray = [
  {FullName: "Person1",PersonId: "id1"},
  {FullName: "Person2",PersonId: "id2"},
  {FullName: "Person3",PersonId: "id3"},
  {FullName: "Person4",PersonId: "id4"}
];

let idsArray = ["id1", "id2", "id3"];

let idsToRemove = new Set(idsArray);
let result = objectArray.filter(o => !idsToRemove.has(o.PersonId));

console.log(result);

Another option is using includes() to test if an array includes a certain string.

let objectArray = [
  {FullName: "Person1",PersonId: "id1"},
  {FullName: "Person2",PersonId: "id2"},
  {FullName: "Person3",PersonId: "id3"},
  {FullName: "Person4",PersonId: "id4"}
];

let idsArray = ["id1", "id2", "id3"];

let result = objectArray.filter(o => !idsArray.includes(o.PersonId));

console.log(result);


Note: If you dont want a new variable, you can override the existing variable as:

objectArray = objectArray.filter(o => ...);
0
BlackBeard On

You can use Vanilla JavaScript's array.filter and array.includes like this:

let objectArray = [
  {FullName: "Person1", PersonId: "id1"},
  {FullName: "Person2", PersonId: "id2"},
  {FullName: "Person3", PersonId: "id3"},
  {FullName: "Person4", PersonId: "id4"}
];
    
  let excludeIdsArray= ["id1", "id2", "id3"];
  
  let newObj = objectArray.filter(obj => !excludeIdsArray.includes(obj.PersonId))
  
  console.log(newObj)

Or, you can use array.reduce and array.includes like this:

let objectArray = [
  {FullName: "Person1", PersonId: "id1"},
  {FullName: "Person2", PersonId: "id2"},
  {FullName: "Person3", PersonId: "id3"},
  {FullName: "Person4", PersonId: "id4"}
];
        
let excludeIdsArray= ["id1", "id2", "id3"];

let newObj = objectArray.reduce((arr, myObject) => {
  if(!excludeIdsArray.includes(myObject.PersonId)) {
    arr.push(myObject)
  } 
  return arr
}, [])
  

console.log(newObj)

0
Nina Scholz On

You could take Except of linq.js with an array of objects and a column parameter to exclude the unwanted PersonId.

var objectArray = [{ FullName: "Person1", PersonId: "id1" }, { FullName: "Person2", PersonId: "id2" }, { FullName: "Person3", PersonId: "id3" }, { FullName: "Person4", PersonId: "id4" }],
    idsArray = ["id1", "id2", "id3"],
    result = Enumerable
        .From(objectArray)
        .Except(
            Enumerable.From(idsArray).Select("{ PersonId: $ }"),
            "$.PersonId"
        )
        .ToArray();

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/linq.js/2.2.0.2/linq.js"></script>