-1

I try to create a test where I send objects to a server, these objects have the same structure and they have a property whose value is a timestamp.

arrayItem: Item[] = [
    {ref: "toto", zone:"zone11", stamp: Date.now()},
    {ref: "tutu", zone:"zone22", stamp: Date.now()},
    {ref: "titi", zone:"zone33", stamp: Date.now()},
    {ref: "truc", zone:"zone44", stamp: Date.now()}
]

I would like to know how I can check if 2 timestamp or more are identical in order to create an alert and prevent sending to server.

to be clearer, if ever a timestamp is identical whatever the position of the object, I would like to generate an error. For now I try to create a nested loop

arrayItem: Item[] = [
        {ref: "toto", zone:"zone11", stamp: 999999999}, 
        {ref: "tutu", zone:"zone22", stamp: 111111111},
        {ref: "titi", zone:"zone33", stamp: 777777777},
        {ref: "truc", zone:"zone44", stamp: 111111111}
]
2
  • 1
    Are you asking how to check if two values are the same? Because that's basic JavaScript; use ===. Or is there more to the question? Commented Aug 9, 2021 at 20:57
  • Sorry for my english, I want to know if in the object array 2 or several timestamp are identical. I try to do that with a nested loop Commented Aug 9, 2021 at 21:18

1 Answer 1

0

Date.now() returns numbers so just compare them with strict equality operator like this:

const arrayItem = [
  { ref: "toto", zone: "zone11", stamp: Date.now() },
  { ref: "tutu", zone: "zone22", stamp: Date.now() },
  { ref: "titi", zone: "zone33", stamp: Date.now() },
  { ref: "truc", zone: "zone44", stamp: Date.now() },
];

const [item1, item2] = arrayItem;

console.log(item1.stamp === +item2.stamp);

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

2 Comments

They are already numbers; there's no need for the unary plus. Also, I'm pretty sure we already have a question about "how to check if two values are equal"...
Thanks for pointing out I confused with new Date

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.