在对象数组中搜索条目匹配变量并检查它是否与其他字段匹配

2024-04-09

所以该程序正在尝试分配一个教室。到目前为止,程序已询问用户班级代码、学生人数以及是否需要计算机,并已使用字段“cRObj.name”、“cRObj.students”和 cRObj.computers 构建了一个数组(因此教室名称将列在 cRObj.name 下,学生人数将列在 cRObj.students 下,以及是否有计算机(是/否)列在 cRObj.computers 下。

我必须搜索对象数组,以尝试找到与用户输入的学生数量相匹配的条目(我想我知道该怎么做)。

但是,之后它需要检查 cRObj.computers 是否等于用户的输入,然后如果前两个匹配,则将条目保存在 cRObj.name 下,我不确定该怎么做。


您可以使用数组.过滤器 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter,这需要一个函数,该函数传递数组的每个项目并返回 true 或 false,并返回一个新数组,该数组仅包含传递给 filter 的函数将返回 true 的项目。

您可以部分应用柯里化函数。柯里化函数示例:

const plus = a => b => a+b;
const plusTen = plus(10);//is a function that takes number b and adds 10 (a is 10)
const eleven = plusTen(1); takes 1 for value of b and 10 for value of a

以下是如何使用柯里化函数进行比较:

const equal = search => value =>
  search === value;
const isFive = equal(5);
//... this will return false, search is 5 and value is 10
//  5===10 is false
if(isFive(10)){

提供一个将从对象获取属性的函数:

const getA = o => (o&&o.a);
getA({a:10};//will return 10
getA({hasNoA:10};//will return undefined

组合所有以创建过滤器函数:

const data = [
  {a:3,b:2},
  {a:4,b:6},
  {a:5,b:8},
  {a:6,b:9},
  {a:7,b:3}
];

//filer function needs a getter (like getA)
// comparer that is a partially applied function that takes a value and compares it
//   with o.something (if getter is getA it'll compare with o.a)
// o is the object that needs to be filtered (like elements of data: data[0]={a:3,b:2})  
const filterFn = getter => comparer => o =>
  comparer(getter(o));

//get a property
const getA = o => (o&&o.a);
//you can write getB I'm sure

// compare search === value
const equal = search => value =>
  search === value;

// compare search <= value
const biggerEqual = search => value =>
  search <= value;

// all data items where item.a === 5
console.log(
  "a is 5",
  data.filter(
    filterFn(getA)(equal(5))//partially apply equal with search of 5
  )
);

//all data items where item.a >= 5
console.log(
  "a equals or bigger 5",
  data.filter(
    filterFn(getA)(biggerEqual(5))//partially apply biggerEqual with search of 5
  )
);

//if you want to compare b with some things you could do
// const getB = o => (o&&o.b);
// data
//  .filter(filterFn(getA)(biggerEqual(5)))//a bigger or equal 5
//  .filter(filterFn(getB)(can you guess?)//b equal 5

// here is an example that takes an array of data and an array of filter functions
//  it'll apply filter on array of data for every function
const filterData = (array, filterFunctions) =>
  filterFunctions.reduce(
    (result,filterFunction)=>result.filter(filterFunction),
    array
  );
// using filterData
console.log(
  "using filterData",
  filterData(data,[
    filterFn(getA)(biggerEqual(5))//partially apply biggerEqual with search of 5
    //,you could add more filters here
  ])
)

对于更复杂的比较器(例如想知道对象的值是否在值列表中),您可以执行以下操作:

const filterFn = getter => comparer => o =>
  comparer(getter(o));
const data= [{id:23},{id:11},{id:435}];
const values= [23, 435, 5];

const getId = o => o.id;
const isIn = haystack => needle => haystack.includes(needle);
const isInValues = isIn(values);
console.log(
  data.filter(filterFn(getId)(isInValues))
)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在对象数组中搜索条目匹配变量并检查它是否与其他字段匹配 的相关文章

随机推荐