Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

subtract same variables in single object javascript

const token = [
  {"token":"d2r4Z62OTGiPyNmdHTUfny",
  "time":1652767811},
  {"token":"dnl13twkQIqifdvaxp1t6e",
  "time":1652767811},
  {"token":"eDZxQu0FSWm72D2-T1md5X",
  "time":1652767811},
  {"token":"dnl13twkQIqifdvaxp1t6e",
  "time":1652767811}]; // Try edit me
  const arr = [];
  for (var i=0; i<=token.length-1; i++)
  {
    const millis = Date.now();
    const time  = Math.floor(millis / 1000);
    if (token[i].time > time) {
      arr.push(token[i].token)
    }
   }
   console.log(arr);

between these two token variables are the same, I want to distinguish them from each other, how can I do that?
Example Output:

const example = [
  "d2r4Z62OTGiPyNmdHTUfny",
  "eDZxQu0FSWm72D2-T1md5X",
  "dnl13twkQIqifdvaxp1t6e"
];
console.log(example);

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Just use a Set as it doesn’t allow for duplicates:

const tokens = [
  {"token":"d2r4Z62OTGiPyNmdHTUfny", "time":1652767811},
  {"token":"dnl13twkQIqifdvaxp1t6e", "time":1652767811},
  {"token":"eDZxQu0FSWm72D2-T1md5X", "time":1652767811},
  {"token":"dnl13twkQIqifdvaxp1t6e", "time":1652767811}
];

const now = Math.floor(Date.now() / 1000);
const result = new Set(
  tokens.filter(({ time }) => time > now).map(({ token }) => token)
);

console.log(...result);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading