1

Hello mates once again,

I try to compare two arrays and do something if they have same components or not. I found here this Using jQuery to compare two arrays Now i try to use it but obvusly im doing something wrong.

This is my JSfiddle where my code is. I put there two alerts to watch my code but it seems there is something wrong with pushing elements to array?

Here is my code:

HTML

<p class="wyliczanka-element">Heh</p>
<p class="wyliczanka-element">huh</p>

JavaScript

$(document).ready(function () {
    var wyliczanka_wzor = [Heh, huh];
    var wyliczanka = [];

    $('.wyliczanka-element').each(function (i) {
        wyliczanka.push($(this).text());
    });
    alert(wyliczanka[0]);

    if (($(wyliczanka_wzor).not(wyliczanka).length === 0) && ($(wyliczanka).not(wyliczanka_wzor).length === 0)) {
        alert("Youp!");
    } else {
        alert("meh");
    }
});

Thanks for any help.

4
  • jQuery's .not() function isn't intended for comparing arrays of strings. What do you expect this: var wyliczanka_wzor = [Heh, huh]; to do? You don't have variables Heh and huh, so that line will give you ReferenceError: Heh is not defined - should it be ... ["Heh", "huh"]? Commented Jul 8, 2013 at 11:13
  • Your fiddle doesn't match this code. Should the Heh and huh in the javascript be in quotes? Commented Jul 8, 2013 at 11:13
  • Also your fiddle is having syntax error Commented Jul 8, 2013 at 11:14
  • try this JSON.stringify( wyliczanka_wzor ) == JSON.stringify( wyliczanka ) . or try lodash.com/docs#isEqual Commented Jul 8, 2013 at 11:14

1 Answer 1

1

You are comparing two strings to two undefined variables.

var wyliczanka_wzor = [Heh, huh];

should be

var wyliczanka_wzor = ['Heh', 'huh'];
Sign up to request clarification or add additional context in comments.

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.