0

I am looking for a simple solution to compare just the "session" values from two different arrays of objects using React.JS.

I have:

  this.state = {
        dataOne: [
            {
                id: "1",
                session: "10:00",
                value: "10:00"
            },
            {
                id: "2",
                session: "10:15",
                value: "10:15"
            },
            {
                id: "3",
                session: "10:30",
                value: "10:30"
            },
            {
                id: "4",
                session: "10:45",
                value: "10:45"
            },
            {
                id: "5",
                session: "11:00",
                value: "11:00"
            },
        ],
        dataTwo: [
            {
                id: "6",
                session: "10:00",
                value: "10:00"
            },
            {
                id: "7",
                session: "10:15",
                value: "10:15"
            },
            {
                id: "8",
                session: "11:30",
                value: "11:30"
            },
            {
                id: "9",
                session: "12:45",
                value: "12:45"
            },
            {
                id: "10",
                session: "13:00",
                value: "13:00"
            },
        ]
    }
}

Those are the two arrays I need to compare.

I tried the following thing:

 if(this.state.dataOne.map(item => item.session) === (this.state.dataTwo.some(item => item.session)) {
       return console.log('true');
 else {
       return console.log('false);
  }

Any suggestions? It always returns false to me....

4
  • console.log always returns undefined Commented Jul 20, 2020 at 12:14
  • Are you trying to compare each session of dataOne with the corresponding index session in the second array? Commented Jul 20, 2020 at 12:16
  • I don't really know what exact you want to compare. Probably it can work for you: dataOne.map(item => item.session).sort().join('') === dataTwo.map(item => item.session).sort().join('') Commented Jul 20, 2020 at 12:18
  • Compare them how? You said you want to compare them, but that's pretty vague. You can compare them in a hundred different ways. Commented Jul 20, 2020 at 12:19

1 Answer 1

1

Array.prototype.map will return an object(array) and Array.prototype.some will return a boolean. Since you are using a strict equal, the expression will always be evaluated to false

const dataOneSession = this.state.dataOne.map(item => item.session);
const dataTwoSession = this.state.dataTwo.map(item => item.session);

let isAllValueMatched = true;

dataOneSession.forEach((value, i) => {
  if(value !== dataTwoSession[i]) {
    isAllValueMatched = false;
  }
});

if(isAllValueMatched) console.log(true);
else console.log(false);

I just corrected the code that you had given, we can write this code in an optimised way if you furnish more information like, are the arrays going to be in same length, will there be a dataThree? etc.

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

4 Comments

It possible without extra map. And some performance optimisations: const isAllValueMatched = dataOne.every(({session}, i) => session === dataTwo[i].session)
Yeah I agree. But cannot give such solutions without knowing, dataTwo gonna be the same size as dataOne. no? Could be resulted in an exception. OP has to comment on it.
The initial idea is that i need to check if one session from the first array is equal to a session in second array.
Yes, you are right. If arrays' length aren't equal, there's no reason to check them: const isAllValueMatched = dataOne.length === dataTwo.length && dataOne.every(({session}, i) => session === dataTwo[i].session)

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.