在JavaScript数组中存储相同的键和不同的值,并使用键值对进行匹配

时间:2016-12-13 14:21:20

标签: javascript jquery arrays

     $(document).on('change', '.units', function () {
            var obj = $(this);
            unit_id = parseInt($(this).val());
            var item_array = [];
            var unit_array = [];
            var units = $(".units");
            var items = $(".items");

            $('#used_items tr').each(function () {
                $(this).find('.items').each(function () {
                    item_array.push($(this).val());
                });
                $(this).find('.units').each(function () {
                    unit_array.push($(this).val());
                });
            });
            var item_unit_associative_array = [];
            for (var i = 0; i < units.length; i++) {
                if (item_unit_associative_array[item_array[i]] == unit_array[i]) {
                    obj.val('');
                    return alert("Given Item Unit Already Selected");
                }
                else {
                    item_unit_associative_array[item_array[i]] = unit_array[i];
                }
            }
            console.log(item_unit_associative_array););

item_array unit_array 我想建立像

这样的新对象
  

var item_unit_associative_array = [1:12,1:13,1:14,2:10];

最后要检查的对象是否包含键:值,如

  

var test = {1:12}

是否存在 item_unit_associative_array

1 个答案:

答案 0 :(得分:1)

我认为您需要嵌套两层对象。请参阅结构的注释:

&#13;
&#13;
<?php
/** GLOBAL VARIABLES (START) */
    $client = new SoapClient('***'); // Magento API URL.
    $session_id = $client->login('***', '***'); // Standard API 'User Name' and 'API Key'. 
/** GLOBAL VARIABLES (END) **/

/** CSV (START) */
    $csv = array(); // ?
    $line = array(); // ?
    // ?
    if (FALSE !== $handle = fopen("test.csv", "r")) {
        while (FALSE !== $row = fgetcsv($handle)) {
            $csv[] = $row;
        }
    }
    // ?
    foreach (array_slice($csv, 1) as $row) {
        $new_row = array();
        for ($i = 0, $c = count($csv[0]); $i < $c; ++$i) {
            $new_row[$csv[0][$i]] = $row[$i]; 
        }
        $line[] = $new_row;
    }
/** CSV (END) */

/** CSV SKU CONVERSION (START) */
    $csv_product_list = ''; // Create $csv_product_list variable before results of the loop are added.

    // Loop through each CSV row and assign the results to $csv_product_list.
    foreach ($line as $iLineNumber => $data_line) {
        $data_line_without_change = $data_line;

        if (!empty($data_line['Product SKU'])) {
                $csv_product_list .= $data_line['Product SKU'] . ' ';
        }
    }
/** CSV SKU CONVERSION (END) */

/** ZOEY PRODUCTS (START) */
    // Filter for where the 'FMA Stock' attribute is set to 'Yes'.
    $fma_stock_filter = array('complex_filter'=>
        array(
            array('key'=>'fma_stock', 'value'=>array('key' =>'eq', 'value' => 'Yes')),
        ),
    );
    // Retrieve list of products using the filter and assign the result to $zoey_product_list.
    $zoey_product_list = $client->catalogProductList($session_id, $fma_stock_filter);

    // Convert the result from $zoey_product_list into an array of SKU's as $sku.
    $sku = [];
    foreach ($zoey_product_list as $item) {
        $sku[] = $item->sku;
    }
/** ZOEY PRODUCTS (END) */

    echo 'The current contents of <b>$csv_product_list</b> are ', $csv_product_list, '<br><br>';

/*** DISABLE PRODUCTS (START) */
    for ($s = 0; $s < count($sku); $s++) {
        if (strpos($csv_product_list, $sku[$s] !== false)) {
            echo '<b>', $sku[$s], '</b> does exist within <b>$csv_product_list</b><br>';
        } else {
            echo '<b>', $sku[$s], '</b> does not exist within <b>$csv_product_list</b><br>';
        }
    }
/*** DISABLE PRODUCTS (END) */
?>
&#13;
&#13;
&#13;

如果您不喜欢嵌套及其使测试复杂化的方式,您还可以&#34; stringify&#34; item-unit id组合:

&#13;
&#13;
var items = [ 1,  1,  1,  2];
var units = [12, 13, 14, 10];

// Create the object like so:
// { 
//   1: { 12: true, 13: true, 14: true },
//   2: { 10: true }
// }
var itemUnitAssociativeObject = {};
units.forEach(function(unitId, i) {
  var itemId = items[i];  
  if (!itemUnitAssociativeObject[itemId]) {
    itemUnitAssociativeObject[itemId] = {};
  }
  
  itemUnitAssociativeObject[itemId][unitId] = true;
});

console.log("1:13", test(1, 13));
console.log("1:10", test(1, 10));
console.log("2:10", test(2, 10));

function test(item, unit) {
   return !!(itemUnitAssociativeObject[item] &&
     itemUnitAssociativeObject[item][unit]);
}
  
&#13;
&#13;
&#13;

相关问题