【问题标题】:How to map/filter and match a string in array如何映射/过滤和匹配数组中的字符串
【发布时间】:2021-01-16 10:41:39
【问题描述】:

所以我有一个数组:

pr = <?php echo json_encode($users); ?>;

试图映射它们:

pro = pr.map(it=> it.name); //usernames
pro1 = pr.map(it=> it.time); //date+time

尝试删除重复的名称:

var filteredArr = pro.filter(function(item, index) {
if (pro.indexOf(item) == index)
return item;
});

它正在工作,但我想添加更多过滤器。我有像“Huan”和“Húan”这样的名字。有重复,但是用这种方法,我无法删除它们。

我的最终目标是,做一个“if”语句,过滤日期和名字。

所以逻辑:

if(今天的日期 === 数组日期) 返回名称;

输出:

公关:

//with alert(JSON.stringify(pr));

[{"name":"john wick","time":"1 月 16 日 10:27 AM"},{"name":"huan","time":"1 月 16 日 10:28 AM"},{"name":"húan","time":"1 月 16 日上午 10:28"},{"name":"húan","time":"1 月 16 日上午 14:28" }]

专业人士:

//with alert(JSON.stringify(pro));

["约翰威克","huan","húan","húan"]

pro1:

//with alert(JSON.stringify(pro1));

["1 月 16 日上午 10:27","1 月 16 日上午 10:28","1 月 16 日上午 10:28 上午","1 月 16 日上午 14:28"]

filteredArr:

//with alert(JSON.stringify(filteredArr));

["约翰威克","huan","húan"]

所以我的问题是,我怎样才能显示名称(仅“链接”到今天的日期)?

【问题讨论】:

  • 您似乎在问两个问题:删除与重音相关的“重复项”,以及过滤当前日期。请只问一个问题。另外,这里似乎 PHP 无关紧要,所以请删除 PHP 代码,并用纯 JavaScript 代码替换它。

标签: javascript arrays regex date


【解决方案1】:

最好将姓名和日期放在一起,直到最后一刻。首先过滤当前日期的数据。由于日期格式不是 ISO YYYY-MM-DDTHH:mm:nn 格式,您需要手动对其进行解析。获得过滤后的对象列表后,您可以像使用.map() 一样提取名称。您只保留唯一值的功能是正确的,但对于较大的集合,您将使用 [...new Set(names)] 获得更好的性能。

所以这里有一个 sn-p 这样做:

let data = [{"name":"john wick","time":"January 16, 10:27 AM"},{"name":"huan","time":"January 16, 10:28 AM"},{"name":"húan","time":"January 16, 10:28 AM"},{"name":"húan","time":"January 16, 14:28 AM"}];

let usernames = [...new Set(
    data.filter(function ({time}) {
        let [monthName, date] = time.match(/\w+/g); // get first two words
        let month = "JanFebMarAprMayJunJulAugSepOctNovDec".indexOf(monthName.slice(0, 3)) / 3;
        let now = new Date();
        return now.getMonth() === month && now.getDate() === +date;
    }).map(({name}) => name)
)];

console.log(usernames);

对于另一个问题,考虑到“huan”和“húan”应该相等:您必须转换您认为相等的字母。见Efficiently replace all accented characters in a string?

重音字符的例子非常少,看起来像这样:

let data = [{"name":"john wick","time":"January 16, 10:27 AM"},{"name":"huan","time":"January 16, 10:28 AM"},{"name":"húan","time":"January 16, 10:28 AM"},{"name":"húan","time":"January 16, 14:28 AM"}];

// extend as needed:
let accents = { "à": "a", "è": "e", "ì": "i", "ò": "o", "ù": "u",
                "á": "a", "é": "e", "í": "i", "ó": "o", "ú": "u" }; 
let regex = RegExp("[" + Object.keys(accents).join("") + "]", "g");

let usernames = [...new Set(
    data.filter(function ({time}) {
        let [monthName, date] = time.match(/\w+/g); // get first two words
        let month = "JanFebMarAprMayJunJulAugSepOctNovDec".indexOf(monthName.slice(0, 3)) / 3;
        let now = new Date();
        return now.getMonth() === month && now.getDate() === +date;
    }).map(({name}) => name.replace(regex, m => accents[m]))
)];

console.log(usernames);

【讨论】:

    猜你喜欢
    • 2021-06-05
    • 2021-10-08
    • 2022-01-14
    • 2021-11-15
    • 2018-11-27
    • 1970-01-01
    • 2023-03-19
    • 2022-01-02
    • 2021-12-15
    相关资源
    最近更新 更多