简体   繁体   English

过滤器数组仅使用标题创建新数组

[英]filter array creating new array with title only

Using an array of objects: 使用对象数组:

[{... title:'sss'},
{... title:'esd'},
{... title:'ccc'},
{... title:'vvv'},
{... title:'bb'},
{... title:'ess'}]

I'd like to search elements and return an array containing only the title of each result, something like: 我想搜索元素并返回一个仅包含每个结果标题的数组,如下所示:

 const filtered = data.filter(function(el) {
                                return el.title === text;
                            }).map(title => title); 

So if my search text was es I would end up with ['esd','ess'] 因此,如果我的搜索文字是es我将以['esd','ess']结尾

你快到了

const filtered = data.filter(el => el.title === text).map(el => el.title)

You could use reduce to execute only one iteration over the initial array, rather than two iterations ( filter and map ) for performance reasons: 出于性能原因,您可以使用reduce在初始数组上仅执行一次迭代,而不是两次迭代( filtermap ):

const filtered = data.reduce((acc, val) => {
    if (val.title === text)
        acc.push(val.title);        
    return acc;
}, []);

NOTE : There is no point of filtering against specific title and then returning an array of just the title. 注意 :没有必要针对特定​​标题进行过滤,然后仅返回标题数组。 It is sure that you are getting an array of the same text . 确保您得到的是相同文本的数组 Maybe, you could filter against another property, or include another property to the outputed array just to distinguish the resulting items. 也许,您可以针对另一个属性进行过滤,或者将另一个属性包括在输出数组中只是为了区分结果项。

You could also like to read about transducers that perform this task. 您还可能想了解执行此任务的换能器

As per this, 按照这个

So if my search text was es I would end up with ['esd','ess'] 因此,如果我的搜索文字是es我将以['esd','ess']结尾

You need this , 你需要这个

const filtered = data.filter(function(el) { return el.title.includes(text);}).map(el => el.title); 
const filtered = data.map(obj => obj.title);

You can use Array.reduce() and String.indexOf() : 您可以使用Array.reduce()String.indexOf()

data.reduce((acc, { title }) => title.indexOf(text) > -1 ? (acc.push(title), acc) : acc, []);

Demo: 演示:

 const data = [{name: 'aa', title:'sss'}, {name: 'ee', title:'esd'}, {name: 'cc', title:'ccc'}, {name: 'vv', title:'vvv'}, {name: 'bb', title:'bb'}, {name: 'ss', title:'ess'}]; function filterData(data, text) { return data.reduce((acc, { title }) => title.indexOf(text) > -1 ? (acc.push(title), acc) : acc, []); } const filtered = filterData(data, 'es'); console.log(filtered); 

Just to share something a bit different, here is an approach relying on a single iteration using function generators . 只是共享一些不同之处,这是一种使用函数生成器依靠一次迭代的方法。

This method requires no filter and mapping, it just iterates the array, acceps a filter callback function and map callback function and yields the mapped function result if the filter function result is truthy. 此方法不需要过滤器和映射,它仅对数组进行迭代,接​​受过滤器回调函数和映射回调函数,如果过滤器结果为真,则生成映射函数结果。

 const data = [{name: 'aa', title:'sss'}, {name: 'ee', title:'esd'}, {name: 'cc', title:'ccc'}, {name: 'vv', title:'vvv'}, {name: 'bb', title:'bb'}, {name: 'ss', title:'ess'} ]; function* filterAndMap(arr, filterCallback, mapCallback) { for (const item of arr) { if (filterCallback(item)) yield mapCallback(item); } } const res = [...filterAndMap(data, ({title}) => title.indexOf('ss') > -1, ({title}) => title)]; console.log(res); 

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

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