简体   繁体   English

jQuery:比较二维数组和一维数组

[英]jQuery: Compare 2D array with 1D array

Let's say I have 2 javascript/jQuery arrays in my code.假设我的代码中有 2 个 javascript/jQuery arrays。

One 2D array that looks like this (console output):一个二维数组,如下所示(控制台输出):

0: {title: 'Freemium', modules: 'Ordering'}
1: {title: 'Standaard', modules: 'Ordering,Invoicing,Cash system'}

and one is a standard 1D array that looks like this (console output):一个是标准的一维数组,如下所示(控制台输出):

['Ordering', 'Invoicing', 'Cash system']

I want to compare both arrays to check if the second array matches one of the first arrays. If it does, it needs to return the first array's title column.我想比较两个 arrays 以检查第二个数组是否与第一个 arrays 中的一个匹配。如果匹配,它需要返回第一个数组的标题列。

To put it into context: Users can select a number of Modules, and when all the selected modules match one of the configured Pricing Plans, the name of the pricing plan is returned.换句话说:用户可以 select 多个模块,当所有选择的模块匹配配置的一个定价计划时,返回定价计划的名称。

How would I go about this?我怎么会go这个呢? I already have both arrays ready in my code, I just need to find a way to compare them and return the first array's title column.我的代码中已经准备好了 arrays,我只需要找到一种方法来比较它们并返回第一个数组的标题列。 I found some solutions to compare two arrays, but never a 2D with a 1D array.我找到了一些解决方案来比较两个 arrays,但从来没有将 2D 与 1D 数组进行比较。

UPDATE:更新:

So my question is perhaps not very well worded or technically correct.所以我的问题可能措辞不佳或技术上不正确。

The different plans are added on the page like this:页面上添加了不同的计划,如下所示:

<div id="packages">
    <div class="package" data-title="Freemium" data-modules="Ordering"></div>
    <div class="package" data-title="Standaard" data-modules="Ordering,Invoicing,Cash system"></div>
</div>

I am using the following JS code to 'add' the packages to my code:我正在使用以下 JS 代码将包“添加”到我的代码中:

$("#packages .package").each(function(index) {
   var package_title = $(this).data('title');
   var package_modules = $(this).data('modules');
   packages_array.push({"title": package_title, "modules": package_modules});
});

Each by the user selected module is added to an array like this:每个由用户选择的模块都添加到这样的数组中:

$(".modules .module.active").each(function() {    
   modules_array.push($(this).data('title'));
});

(The arrays packages_array and modules_array are declared at the top of my code) (arrays packages_arraymodules_array在我的代码顶部声明)

This is what I have right now, and if someone could point me in the right direction on how to improve my code and how to compare both arrays..这就是我现在所拥有的,如果有人能指出正确的方向,告诉我如何改进我的代码以及如何比较两者 arrays..

You could first use the array join() function, to convert your selectors into a comma separated string, them use a forEach() function in your 2d array and compare each module to your new string value您可以首先使用数组join() function,将您的选择器转换为逗号分隔的字符串,它们在您的二维数组中使用forEach() function 并将每个模块与您的新字符串值进行比较

Edit: To account for unsorted arrays you can first order your selectors arrays with sort() and split your modules using split(',') , order the resulting array, and join() it, resulting in two sorted strings that can be compared编辑:要考虑未排序的 arrays,您可以先使用sort()对选择器 arrays 进行排序,然后使用split(',')拆分模块,对结果数组进行排序,然后对其进行join() ,从而得到两个可以比较的排序字符串

 let objectArray = [{ title: 'Freemium', modules: 'Ordering' }, { title: 'Standaard', modules: 'Ordering,Invoicing,Cash system' } ] let selectorsArray = ["Ordering", "Invoicing", "Cash system"] let selectorsString = selectorsArray.sort().join() objectArray.forEach((plan) => { let sortedModules = plan.modules.split(",").sort().join() if (sortedModules === selectorsString) console.log("found: " + plan.title) })

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

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