映射、过滤和自己的价值

map, filter and own value

本文关键字:自己的 过滤 映射      更新时间:2023-09-26

我有:stackoverflow.com/questions/19012903/get-value-from-multi-object但是我修改了它,我想添加到超过 4 个字符的结果字符串中。

function isLong(val)
{
    if(val.length > 4){
       return true;
    } else {
        return false;
    }
}
var page = [{
    title: 'aaa',
    text: '111'
}, {
    title: 'bbb',
    text: '222'
}, {
    title: 'ccc',
    text: '333'
}, {
    title: 'ddd',
    text: '444'
}, {
    title: 'eee',
    text: '444'
}];
console.log([].concat.apply([], '222, 333, 4441, long1, long, long2'.split(', ').map(function (t) {
    return page.filter(function (o) {
        return o.text === t || isLong(t);
    }).map(function (c) {
        return c.title
    });
})).join(", "));

斯菲德尔

但这让我返回了所有价值。我尝试在行中检查此内容返回 o.text === t ||是长(t);

对于这个例子,我希望收到:

BBB, CCC, LONG1, Long2

BBB 和 CCC 来自对象页面。 long1 和 long2 是我的自定义字符串,用逗号分隔。

var str = '222, 333, 4441, long1, long, long2';
var strArray = str.split(', ');
var result = strArray.map(function(s){
    return isLong(s) ? s : page.filter(function(o){ // if word has > 4 chars return word else try to match with page array
       return o.text == s; 
    }).map(function(c){
      return c.title;  // if matched return title
    })[0]; // select first match
}).filter(function(u){
    return u;  // remove undefined results
});
console.log(result);

示例:http://jsfiddle.net/YrZNq/6/