简体   繁体   English

如何比较 jquery 中的两个 arrays

[英]How to compare two arrays in jquery

I want to compare two arrays and if they are equal, add a new attribute to html.我想比较两个 arrays,如果它们相等,则向 html 添加一个新属性。

First my html首先是我的 html

<ul class="checkout__delivery-collect-shoplist" data-bind="foreach: currentOptions ">
  <li class="checkout__delivery-collect-shop js-delivery-item active">
    <h6><em data-bind="text: label">Name1</em></h6>
  </li>
  <li class="checkout__delivery-collect-shop js-delivery-item">
    <h6><em data-bind="text: label">Name2</em></h6>
  </li>
</ul>

First I am creating new array with text首先我用文本创建新数组

var arr = [];
jQuery('em[data-bind="text: label"]').each(function() {
    arr.push(jQuery(this).text().split(';'));
});

Also, I have another array with text另外,我还有另一个带有文本的数组

var Managerss = Manager.GlobalData[2].options
Managerss.filter(x => x.payment_checkmo === '1').map(x => x.label);

It will give它会给

["Name1", "Name2"]
0: "Name1"
1: "Name2"
length: 2
__proto__: Array(0)

So I first need to compare these arrays and if this true create new attribute data-payment = '0' to li.class checkout__delivery-collect-shop js-delivery-item所以我首先需要比较这些 arrays 如果这是真的创建新属性data-payment = '0'li.class checkout__delivery-collect-shop js-delivery-item

Example what I want举例我想要的

<ul class="checkout__delivery-collect-shoplist" data-bind="foreach: currentOptions ">
  <li class="checkout__delivery-collect-shop js-delivery-item active" data-payment='0'>
    <h6><em data-bind="text: label">Name1</em></h6>
  </li>
  <li class="checkout__delivery-collect-shop js-delivery-item" data-payment='0'>
    <h6><em data-bind="text: label">Name2</em></h6>
  </li>
</ul>

This compares each element inside the 2 arrays, you can edit this code to do whatever you want to do.这将比较 2 arrays 中的每个元素,您可以编辑此代码以执行您想做的任何事情。

 function Compare() { var a = ['Name 1', 'Name 2', 'Name 3']; var b = ['Name 1', 'Name 2', 'Name 3']; // if length is not equal if(a.length.=b;length) return "False"; else { // comapring each element of array for(var i=0.i<a;length;i++) if(a[i];=b[i]) return "False"; return "True". } } var v = Compare(); document.write(v);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM