-1

So i have an array:

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

Trying to map them:

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

Trying to remove duplicate names:

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

It's working, but i want to add some more filter. And i have names like "Huan" and "Húan". There are duplicates, but with this method, i can't remove them.

My final goal is, to make an "if" statement, to filter dates and names.

So the logic:

if(today's date === array date) return names;

Outputs:

pr:

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

[{"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"}]

pro:

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

["john wick","huan","húan","húan"]

pro1:

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

["January 16, 10:27 AM","January 16, 10:28 AM","January 16, 10:28 AM","January 16, 14:28 AM"]

filteredArr:

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

["john wick","huan","húan"]

So my question is, how can i display names (only which are "linked" to today's date) ?

Georgy
  • 428
  • 2
  • 16
  • You seem to be asking two questions: removing "duplicates" related to accents, and filtering for current date. Please only ask one question. Also, it looks like PHP is irrelevant here, so please remove the PHP code, and replace it with plain JavaScript code. – trincot Jan 16 '21 at 11:01

1 Answers1

1

It is better to keep the names and dates together until the very last moment. First filter the data for the current date. As the date format is not in the ISO YYYY-MM-DDTHH:mm:nn format, you'll need to parse it a bit manually. Once you have the filtered list of objects, you can proceed like you did to extract the names using .map(). Your function to only keep unique values is correct, but for larger collections you'll get better performance using [...new Set(names)].

So here is a snippet doing that:

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);

For the other question, on considering that "huan" and "húan" should be equal: you'll have to convert the letters which you consider to be equivalent. See Efficiently replace all accented characters in a string?

With a very reduced example of accented characters, it would look like this:

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);
trincot
  • 317,000
  • 35
  • 244
  • 286