最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Filter one array using values from another array using linqjs - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question edited Jul 3, 2018 at 20:58 user7637745 9852 gold badges14 silver badges27 bronze badges asked Jul 3, 2018 at 16:11 goediazgoediaz 6526 silver badges20 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 4

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)

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 => ...);

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.

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./ajax/libs/linq.js/2.2.0.2/linq.js"></script>

发布评论

评论列表(0)

  1. 暂无评论