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

IT技术 javascript arrays search javascript-objects
2021-03-13 15:24:28

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

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

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

1个回答

您可以使用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))
)