0

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

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

4 Comments

Wow, this is cool. I've never seen this before. I will research how this works.
Thank you I modified the question, the code used inside forEach, so it brings me the result more than once, so what is the solution? See the question again after adding the code inside forEach
@MedhatFarid not sure what you are doing in the new code. I used console.log just to show the result..you don't need to use the console.log as such
Just use it to modify the code, the problem now is that the result is repeated
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);

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.