Typescript 基于数组的对象数组过滤

Typescript 基于数组的对象数组过滤,typescript,Typescript,我正在尝试筛选一组如下所示的对象 const ELEMENT_DATA: PeriodicElement[] = [ { position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H' }, { position: 2, name: 'Helium', weight: 4.0026, symbol: 'He' }, { position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li

我正在尝试筛选一组如下所示的对象

const ELEMENT_DATA: PeriodicElement[] = [
  { position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H' },
  { position: 2, name: 'Helium', weight: 4.0026, symbol: 'He' },
  { position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li' },
  { position: 4, name: 'Beryllium', weight: 9.0122, symbol: 'Be' },
  { position: 5, name: 'Boron', weight: 10.811, symbol: 'B' }
];
const values = [1,5];
const NEW_VALUES: PeriodicElement[] = [
  { position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H' },
  { position: 5, name: 'Boron', weight: 10.811, symbol: 'B' }
];
和一个如下所示的数组

const ELEMENT_DATA: PeriodicElement[] = [
  { position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H' },
  { position: 2, name: 'Helium', weight: 4.0026, symbol: 'He' },
  { position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li' },
  { position: 4, name: 'Beryllium', weight: 9.0122, symbol: 'Be' },
  { position: 5, name: 'Boron', weight: 10.811, symbol: 'B' }
];
const values = [1,5];
const NEW_VALUES: PeriodicElement[] = [
  { position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H' },
  { position: 5, name: 'Boron', weight: 10.811, symbol: 'B' }
];
我需要的是过滤
元素数据
新值
如下所示

const ELEMENT_DATA: PeriodicElement[] = [
  { position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H' },
  { position: 2, name: 'Helium', weight: 4.0026, symbol: 'He' },
  { position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li' },
  { position: 4, name: 'Beryllium', weight: 9.0122, symbol: 'Be' },
  { position: 5, name: 'Boron', weight: 10.811, symbol: 'B' }
];
const values = [1,5];
const NEW_VALUES: PeriodicElement[] = [
  { position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H' },
  { position: 5, name: 'Boron', weight: 10.811, symbol: 'B' }
];
我尝试过这样的过滤器:

  filterData(locationName: any) {
    return ELEMENT_DATA.filter(object => {
      return object.position === locationName;
    });
  }
 filterData(locationName: number[]) {
    return ELEMENT_DATA.filter(object => {
      return locationName.includes(object.position);
    });
  }

但我总是得到一个空数组

如果
locationName
作为输入
[1,5]
获取,则代码应如下所示:

  filterData(locationName: any) {
    return ELEMENT_DATA.filter(object => {
      return object.position === locationName;
    });
  }
 filterData(locationName: number[]) {
    return ELEMENT_DATA.filter(object => {
      return locationName.includes(object.position);
    });
  }

如果
locationName
作为输入
[1,5]
获取,则代码应如下所示:

  filterData(locationName: any) {
    return ELEMENT_DATA.filter(object => {
      return object.position === locationName;
    });
  }
 filterData(locationName: number[]) {
    return ELEMENT_DATA.filter(object => {
      return locationName.includes(object.position);
    });
  }
你可以用

或使用下划线JS,使用相同的谓词:

_.filter(ELEMENT_DATA, function (object) {
  return locationName.indexOf(object.position) !== -1; // -1 means not present
}
如果您有权访问的ES6集合或polyfill。 此处locationName应为集合的一种类型

ELEMENT_DATA.filter(object=> locationName.has(object.position))
你可以用

或使用下划线JS,使用相同的谓词:

_.filter(ELEMENT_DATA, function (object) {
  return locationName.indexOf(object.position) !== -1; // -1 means not present
}
如果您有权访问的ES6集合或polyfill。 此处locationName应为集合的一种类型

ELEMENT_DATA.filter(object=> locationName.has(object.position))

locationName是您的数字数组吗?如果有的话1。为什么要按任何方式键入?二,。您是否认为,例如,
1===[1,5]
?!locationName是您的数字数组吗?如果有的话1。为什么要按任何方式键入?二,。您是否认为,例如,
1===[1,5]
?!