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

标签 javascript arrays search javascript-objects

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

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

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

最佳答案

您可以使用 array.filter ,这需要一个函数,该函数传递数组的每个项目并返回 true 或 false,并返回一个新数组,该数组仅包含传递给过滤器的函数将返回 true 的项目。

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

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

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

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))
)

关于javascript - 在对象数组中搜索条目匹配变量,并检查它是否与其他字段匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50075142/

相关文章:

javascript - 编码后如何编辑 JSON 数组

javascript - Jquery Array Object - 如何将后缀变量添加到索引名称

search - 解码以下 Solr 查询

java - 如何在字符串中搜索关键字

javascript - 使用 JavaScript 上传文件 - 如何获得更频繁的 onprogress 事件

javascript - 在 Javascript 中通过 onclick 调用多个函数的最佳方法是什么

javascript - XMLHttpRequest().send(value) 发送空白值

javascript - 将树 json 数据填充到下拉框

c - 在 C 中对数组中的值进行排序

django - 在 Django 中使用 ModelChoiceField 进行搜索