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

119
Vistas
javascript foreach loop group by values

For example, given this array of objects:

[ 
    { userid: "5", articleid: "3"},
    { userid: "5", articleid: "3"},
    { userid: "5", articleid: "3"},
    { userid: "1", articleid: "2"}
]

I want to display the values ​​this way Without repetition inside the loop:

[ 
    { userid: "5", articleid: "3"},
    { userid: "1", articleid: "2"}
]

The code used is javascript

var newMessage = '';
    function realTime(){
        db.collection('chat').where('userid', '==', <?php echo $id; ?>)
        .orderBy('time')
        .onSnapshot(function(snapshot) {
            newMessage = '';
            snapshot.docChanges().forEach(function(change) {
              if (change.type === "added") {
                //console.log(change.doc.data());
                const elements = [change.doc.data()];
                console.log([...new Set(elements.map(JSON.stringify))].map(JSON.parse));
              }
            });

            if (chatHTML != newMessage) {
                $('.msg_body').append(newMessage);
            }
        });
    }
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

This is a an easy way to do it.

const elements = [ 
    { userid: "5", articleid: "3"},
    { userid: "5", articleid: "3"},
    { userid: "5", articleid: "3"},
    { userid: "1", articleid: "2"}
];
console.log([...new Set(elements.map(JSON.stringify))].map(JSON.parse));

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use filter on your array to do it.

The first one allows you to filter only by userID, and the second one with both values to separate also those who are not really duplicates.

const values = [
  { userid: "5", articleid: "3" },
  { userid: "5", articleid: "3" },
  { userid: "5", articleid: "3" },
  { userid: "5", articleid: "2" },
  { userid: "1", articleid: "2" },
];

const first = values.filter(
  (element, index, array) =>
    array.findIndex(
      (otherElement) => element.userid === otherElement.userid
    ) === index
);

const second = values.filter(
  (element, index, array) =>
    array.findIndex(
      (otherElement) =>
        element.userid === otherElement.userid &&
        element.articleid === otherElement.articleid
    ) === index
);

console.log("first", first);
console.log("second", second);

By searching a little more, there was already the answer on several posts.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use Set with filter to achieve the desired result.

const arr = [
  { userid: "5", articleid: "3" },
  { userid: "5", articleid: "3" },
  { userid: "5", articleid: "3" },
  { userid: "1", articleid: "2" },
];

const set = new Set();

const result = arr.filter(({ userid, articleid }) => {
  if (set.has(`${userid}|${articleid}`)) return false;
  else {
    set.add(`${userid}|${articleid}`);
    return true;
  }
});

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