简体   繁体   English

Javascript使用过滤器/包含在一个对象数组上

[英]Javascript using filter/includes on an array of objects

What is the best way to filter out data that exists within an object? 过滤掉对象中存在的数据的最佳方法是什么?

I was able to do use the below code when data was just an array of values but now I need to filter out any data where the item.QID exists in my array of objects. data只是一个值数组时,我能够使用下面的代码,但现在我需要filteritem.QID存在于我的对象数组中的任何数据。

Data Obj: 数据对象:

var data = [{
  QID: 'ABC123',
  Name: 'Joe'
},
{
 QID: 'DEF456',
 Name: 'Bob
}]

Snippet: 片段:

// I don't want to include data if this QID is in my object
this.employees = emp.filter(item =>!this.data.includes(item.QID));

From what I understand, includes only works on an array so I need to treat all of the QID values in my object as an array. 根据我的理解, includes仅适用于数组,因此我需要将对象中的所有QID值视为数组。

Desired Outcome: (assuming item.QID = ABC123 ) 期望的结果:(假设item.QID = ABC123

this.employees = emp.filter(item =>!this.data.includes('ABC123'));

Result: 结果:

var data = [{
  QID: 'DEF456',
  Name: 'Bob'
}]

UPDATE: Apologies, I left some things a little unclear trying to only include the necessary stuff. 更新:道歉,我留下了一些不清楚的东西,试图只包括必要的东西。

// People Search
    this.peopleSearchSub = this.typeahead
        .distinctUntilChanged()
        .debounceTime(200)
        .switchMap(term => this._mapsService.loadEmployees(term))
        .subscribe(emp => {
            // Exclude all of the current owners
            this.employees = emp.filter((item) => item.QID !== this.data.QID);
        }, (err) => {
            this.employees = [];
        });

The above code is what I am working with. 上面的代码就是我正在使用的代码。 data is an object of users I want to exclude from my type-ahead results by filtering them out. data是我希望通过过滤掉我的预先输入结果的用户的对象。

The question is a little ambiguous, but my understanding (correct me if I'm wrong), is that you want to remove all items from a list emp that have the same QID as any item in another list data ? 这个问题有点含糊不清,但是我的理解(如果我错了,请纠正我),是否要删除列表emp中与其他列表data任何项目具有相同QID的所有项目?

If that's the case, try: 如果是这种情况,请尝试:

this.employees = emp.filter(item => !this.data.some(d => d.QID === item.QID))

some is an array method that returns true if it's callback is true for any of the arrays elements. some是一个数组方法,如果任何数组元素的回调为true,则返回true。 So in this case, some(d => d.QID === item.QID) would be true if ANY of the elements of the list data have the same QID as item . 因此,在这种情况下,如果列表data任何元素与item具有相同的QID,则some(d => d.QID === item.QID)将为真。

试试Object#hasOwnProperty()

this.employees = emp.filter(item =>item.hasOwnProperty('QID'));

You can use a for ... in to loop through and filter out what you want: 您可以使用for ... in循环并过滤掉您想要的内容:

const data = [{
    QID: 'ABC123',
    Name: 'Joe'
},
 {
   QID: 'DEF456',
   Name: 'Bob'
 }]

let newData     = [];
let filterValue = 'ABC123';

for (let value in data) {
  if (data[value].QID !== filterValue) {
    newData.push(data[value]);
  }
}

newData will be your new filtered array in this case 在这种情况下, newData将是您新的过滤数组

You can use an es6 .filter for that. 您可以使用es6 .filter。 I also added a couple of elements showing the filtered list and an input to allow changing of the filtered value. 我还添加了一些显示已过滤列表的元素和一个允许更改过滤值的输入。 This list will update on the click of the button. 此列表将在单击按钮时更新。

 const data = [{ QID: 'ABC123', Name: 'Joe' }, { QID: 'DEF456', Name: 'Bob' }] displayData(data); function displayData(arr) { let str = ''; document.getElementById('filterList').innerHTML = ''; arr.forEach((i) => { str += "<li>" + i.QID + ": " + i.Name + "</li>"}) document.getElementById('filterList').innerHTML = str; } function filterData() { let filterValue = document.getElementById('filterInput').value; filterText (filterValue); } function filterText (filterValue) { let newArr = data.filter((n) => n.QID !== filterValue); displayData(newArr) } 
 <input id="filterInput" type="text" value="ABC123" /> <button type ="button" onclick="filterData()">Filter</button> <hr/> <ul id="filterList"><ul> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM