Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

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

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

The easiest way with your current logic is that you can use includes to check token with the arr results. If it's in arr, we don't need to push it to arr.

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

about 4 years ago · Juan Pablo Isaza Denunciar

0

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

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda