简体   繁体   English

如何解决错误,filter() 不是函数

[英]How do I solve the error, filter() is not a function

Here is what I tried.这是我尝试过的。 Also order is part of a query so this works, I am just not posting the rest of the code so it doesn't get messy. order也是查询的一部分,所以这是有效的,我只是不发布其余的代码,所以它不会变得混乱。 I was getting errors on the paymentsResults[i].filter() saying it is not function .我在paymentsResults[i].filter()上遇到错误,说它不是 function 。 Anyways, if there is a better way to write it, please let me know.无论如何,如果有更好的写法,请告诉我。 Appreciate it.欣赏它。

 const getCatType = order => {
  if (
    order.type === "Invoice" ||
    order.type === "One-time" ||
    order.type === "Free"

  ) {
    return "Once";
  } else if (order.type === "Semester") {
    return "Recurring";
  }
};

for (let i = 0; i < paymentsResults.length; i++) {
const paymentsOnce = paymentsResults[i].filter(o => getCatType(o) === "Once");
} 

I assume you are confused with what filter() is actually supposed to do.我假设您对filter()实际应该做什么感到困惑。

filter() is an Array-Function iterating over the given Array and returning the objects that fit the filter-conditions defined in the callback function. filter() 是一个数组函数,它迭代给定的数组并返回符合回调函数中定义的过滤条件的对象。

Therefore you don't need the for-Loop to get into each Item of your array, but simply call your filter-function on the array itself.因此,您不需要 for-Loop 进入数组的每个 Item,而只需在数组本身上调用过滤器函数。

This only applies if you have an Array of Objects with the key type in paymentsResults.这仅适用于在 PaymentsResults 中具有键type的对象数组。 Sadly you didn't provide your origin data-structure.遗憾的是你没有提供你的原始数据结构。 If it is not like I assumed, then please comment down below, what your data structure looks like.如果它不像我假设的那样,那么请在下面评论,你的数据结构是什么样的。

for (let i = 0; i < paymentsResults.length; i++) {
const paymentsOnce = paymentsResults[i].filter(o => getCatType(o) === "Once");
} 

Will become:会变成:

// ONLY GIVEN IF YOUR STRUCTURE LOOKS LIKE 
// paymentsResults = [{type: 'Thing'}, {type: 'Thing'}, ...];
const paymentsOnce = paymentsResults.filter(o => getCatType(o) === "Once");

Using a switch would be more useful.使用switch会更有用。 And then all you need to do it filter out the objects where they match - no need for an additional loop.然后你需要做的就是filter掉它们匹配的对象 - 不需要额外的循环。

 function getCatType(order) { switch(order.type) { case 'Invoice': case 'One-time': case 'Free': return 'Once'; case 'Semester': return 'Recurring'; default: return null; } } const paymentsResults = [ { type: 'Invoice', id: 1 }, { type: 'Semester', id: 2 }, { type: 'Free', id: 3 }, { type: 'One-time', id: 4 } ]; function typeOfPayment(type) { return paymentsResults.filter(order => { return getCatType(order) === type; }); } console.log(typeOfPayment('Once')); console.log(typeOfPayment('Recurring'));

You can use reduce and also you can check for the types you need directly in the reduce cb, mix this with a regex and you can extract what ever types of orders you want by creating a simple array with the types you need.您可以使用reduce,也可以直接在reduce cb 中检查您需要的类型,将其与正则表达式混合,您可以通过使用您需要的类型创建一个简单的数组来提取您想要的任何类型的订单。 Assuming your paymentsResults are something like this array you can do this:假设您的 PaymentsResults 类似于此数组,您可以执行以下操作:

 const paymentsResults = [ { order: { type: 'Invoice', total: 10 }, price: 2 }, { order: { type: 'One-time', total: 11 } }, { order: { type: 'Free', total: 12 } }, { order: { type: 'Semester', total: 13 } }, ]; let extract = ['Invoice', 'One-time', 'Free']; const regex = new RegExp(extract.join('|'), 'i'); let paymentsOnce = paymentsResults.reduce((acc, obj) => { if (regex.test(obj.order.type)) acc.push(obj); return acc; }, []); console.log(paymentsOnce);

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

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