过滤列表中包含列表的对象的最有效方法是什么?我一直在看下划线的_.filter函数,但这需要使用数组并返回数组。使用列表筛选对象列表 - Javascript
我想采取一个对象,并通过某个词过滤它。
例如,我将如何过滤:
[
{level: "Title 1", details: [
{real: "There we go", fake: "THERE_WE_GO"},
{real: "Where is it", fake: "WHERE_IS_IT"},
{real: "The dog jumped", fake: "THE_DOG_JUMPED"},
] },
{level: "Title 2", details: [
{real: "There it is", fake: "THERE_IT_IS"},
{real: "Car is flying", fake: "CAR_IS_FLYING"},
{real: "Driving is fun", fake: "DRIVING_IS_FUN"}
] },
{level: "Title 2", details: [
{real: "There he is", fake: "THERE_WE_GO"},
{real: "Where is the dog", fake: "WHERE_IS_THE_DOG"},
{real: "The dog died", fake: "THE_DOG_DIED"},
{real: "I am tired", fake: "I_AM_TIRED"},
]}
];
由单词 “the”
,使其返回
[
{level: "Title 1", details: [
{real: "There we go", fake: "THERE_WE_GO"},
{real: "The dog jumped", fake: "THE_DOG_JUMPED"},
] },
{level: "Title 2", details: [
{real: "There it is", fake: "THERE_IT_IS"},
] },
{level: "Title 2", details: [
{real: "There he is", fake: "THERE_WE_GO"},
{real: "Where is the dog", fake: "WHERE_IS_THE_DOG"},
{real: "The dog died", fake: "THE_DOG_DIED"},
]}
];
注意,当这个词过滤器 “的”我还想保留任何有“the”这个词作为其中的一部分的词,例如“there”......我只是去检查单词“the”是否在“the”的对象的“真实”索引中细节的数组。
这不是一个对象,而是一个对象数组。因此,下划线过滤对你来说工作得很好。您当然需要编写过滤功能。你有没有做过任何可以显示代码的尝试? –
ES5中有一个内置的[* filter *](http://ecma-international.org/ecma-262/5.1/#sec-15.4.4.20),为什么你需要这样的underscore.js? – RobG