简体   繁体   English

推送到数组并比较两个数组的JavaScript

[英]Push to array and compare two arrays JavaScript

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. 我在这里发现使用jQuery比较两个数组现在我尝试使用它,但是我做错了事。

This is my JSfiddle where my code is. 这是我的代码所在的JSfiddle 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 HTML

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

JavaScript 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. 谢谢你的帮助。

You are comparing two strings to two undefined variables. 您正在将两个字符串与两个未定义的变量进行比较。

var wyliczanka_wzor = [Heh, huh];

should be 应该

var wyliczanka_wzor = ['Heh', 'huh'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JavaScript 比较两个 arrays 并将在第二个数组中具有相同属性值的项目推送到第一个数组 - JavaScript compare two arrays and push items to the first array that have the same property value in the second array 比较两个 arrays 并将相同的元素推送到一个数组中,将 rest 推送到另一个数组中的 javascript - Compare two arrays and push same element in one array and rest in other array in javascript 数组和JavaScript-如何比较两个数组 - Array and javascript - how to compare two arrays 比较JavaScript中的两个数组 - Compare two arrays in javascript 比较两个 arrays javascript - Compare two arrays javascript Javascript-比较两个数组并将缺少的项添加到第一个数组中 - Javascript - Compare two arrays and add missing items into first array 比较两个 Arrays 对象,如果 Javascript 中的值为真,则返回一个新数组 - Compare Two Arrays Of Objects and return a new array if value is true in Javascript Javascript比较两个不同大小的数组,并返回不在第二个数组中的项目 - Javascript Compare two arrays with different sizes and return the items that are not in second array 比较两个数组并更新Javascript中的第二个数组元素 - Compare two arrays and update second array element in Javascript 比较两个 arrays 并制作另一个与 javascript 不同的数组 - Compare two arrays and make the another array with difference in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM