问题描述
我正在尝试使用javascript中的filter()函数过滤对象.
I'm trying to filter an object using the filter() function in javascript .
我想再次过滤这样的数组:[1615,1616].在代码中将其称为value.verdier.
I want to filter againts an array like this: [1615, 1616]. It's referenced in the code as value.verdier.
数据集是一个大型数组,其中包含具有多个属性的对象,这些属性是通过JSON字符串解析的.数组中的每个对象都有一个名为kat_id的属性.
Dataset is a large array holding objects that have several properties, parsed from a JSON string. Each object in the array has a property named kat_id.
目标是减少数组,使其仅容纳kat_id = 1615或kat_id = 1616的对象.或我在value.verdier中具有的其他任何值.
The goal is to reduce the array so that it only holds objects where kat_id=1615 or kat_id=1616. Or any other value that I have in value.verdier.
我的代码是这样的:
dataset = dataset.filter(function (el)
{
return value.verdier.includes(el.kat_id );
});
我可能错过了一些非常明显的东西.
I'm problably missing something really obvious.
推荐答案
如果值和 value.verdier
中的值都是数字或全部 strings .这是因为 includes
在传递给它的东西和调用它的数组中的条目之间进行了 ===
.因此 [1、2、3] .includes("2")
是 false
, ["1","2","3"].includes(2)
.
The code in your question will do that, if the kat_id
values and the values in value.verdier
are all numbers or all strings. That's because includes
does a ===
between the thing you pass into it and the entries in the array you call it on. So [1, 2, 3].includes("2")
is false
, as is ["1", "2", "3"].includes(2)
.
不过,我假设您不会发布该问题是否有效,所以我假设 kat_id
的值类型和 value.verdier的值类型.
是不同的 —其中一个是数字,另一个是字符串.
I assume you wouldn't be posting the question if it were working, though, so I assume the type of the values of kat_id
and the type of the values in value.verdier
are different — one of them is a number, the other is a string.
如果 kat_id
值为字符串,而 value.verdier
值为数字,则需要解析 kat_id
编号,然后再使用 includes
:
If the kat_id
values are strings and the value.verdier
values are numbers, you need to parse kat_id
to number before using includes
:
return value.verdier.includes(parseInt(el.kat_id, 10));
实时示例:
var value = {
verdier: [1615, 1616]
};
var dataset = [
{kat_id: "1", expected: "omit"},
{kat_id: "2", expected: "omit"},
{kat_id: "1615", expected: "retain"},
{kat_id: "3", expected: "omit"},
{kat_id: "1616", expected: "retain"},
{kat_id: "4", expected: "omit"},
{kat_id: "5", expected: "omit"},
];
dataset = dataset.filter(function (el)
{
return value.verdier.includes(parseInt(el.kat_id, 10));
});
console.log(dataset);
或者,如果 kat_id
的值是数字,而 value.verdier
的值是字符串,则需要先将 kat_id
的数字转换成字符串使用 includes
:
Or if the kat_id
values are numbers and the value.verdier
values are strings, you need to convert the kat_id
numbers to strings before using includes
:
return value.verdier.includes(String(el.kat_id));
实时示例:
var value = {
verdier: ["1615", "1616"]
};
var dataset = [
{kat_id: 1, expected: "omit"},
{kat_id: 2, expected: "omit"},
{kat_id: 1615, expected: "retain"},
{kat_id: 3, expected: "omit"},
{kat_id: 1616, expected: "retain"},
{kat_id: 4, expected: "omit"},
{kat_id: 5, expected: "omit"},
];
dataset = dataset.filter(function (el)
{
return value.verdier.includes(String(el.kat_id));
});
console.log(dataset);
如果值 vary (某些 kat_id
是字符串,另一些是数字,和/或某些 value.verdier
值是字符串和其他是数字),则可能需要 some
,而不是 includes
和类型不敏感的比较:
If the values vary (some kat_id
s are strings and others are numbers, and/or some value.verdier
values are strings and others are numbers), you probably need some
rather than includes
and a type-insensitive comparison:
return value.verdier.some(function(val) { return String(val) === String(el.kat_id); });
实时示例:
var value = {
verdier: [1615, "1616"]
};
var dataset = [
{kat_id: "1", expected: "omit"},
{kat_id: 2, expected: "omit"},
{kat_id: "1615", expected: "retain"},
{kat_id: "3", expected: "omit"},
{kat_id: 1616, expected: "retain"},
{kat_id: "4", expected: "omit"},
{kat_id: 5, expected: "omit"},
];
dataset = dataset.filter(function (el)
{
return value.verdier.some(function(val) { return String(val) === String(el.kat_id); });
});
console.log(dataset);
我在上面坚持使用ES5级代码,因为您的问题似乎只在使用ES5级代码.但是以下是使用ES2015 +的示例:
I've stuck with ES5-level code above because your question seemed to be using ES5-level code only. But here are those examples using ES2015+:
dataset = dataset.filter(el => value.verdier.includes(parseInt(el.kat_id, 10)));
const value = {
verdier: [1615, 1616]
};
let dataset = [
{kat_id: "1", expected: "omit"},
{kat_id: "2", expected: "omit"},
{kat_id: "1615", expected: "retain"},
{kat_id: "3", expected: "omit"},
{kat_id: "1616", expected: "retain"},
{kat_id: "4", expected: "omit"},
{kat_id: "5", expected: "omit"},
];
dataset = dataset.filter(el => value.verdier.includes(parseInt(el.kat_id, 10)));
console.log(dataset);
dataset = dataset.filter(el => value.verdier.includes(String(el.kat_id)));
const value = {
verdier: ["1615", "1616"]
};
let dataset = [
{kat_id: 1, expected: "omit"},
{kat_id: 2, expected: "omit"},
{kat_id: 1615, expected: "retain"},
{kat_id: 3, expected: "omit"},
{kat_id: 1616, expected: "retain"},
{kat_id: 4, expected: "omit"},
{kat_id: 5, expected: "omit"},
];
dataset = dataset.filter(el => value.verdier.includes(String(el.kat_id)));
console.log(dataset);
dataset = dataset.filter(el => value.verdier.some(val => String(val) === String(el.kat_id)));
const value = {
verdier: [1615, "1616"]
};
let dataset = [
{kat_id: "1", expected: "omit"},
{kat_id: 2, expected: "omit"},
{kat_id: "1615", expected: "retain"},
{kat_id: "3", expected: "omit"},
{kat_id: 1616, expected: "retain"},
{kat_id: "4", expected: "omit"},
{kat_id: 5, expected: "omit"},
];
dataset = dataset.filter(el => value.verdier.some(val => String(val) === String(el.kat_id)));
console.log(dataset);
这篇关于使用include的Javascript过滤器对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!