javascript - 在 JavaScript 数组中存储相同的键和不同的值并与键值对匹配

标签 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_arrayunit_array 我想构建新的对象,例如

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

最后想检查一个对象由 key:value 组成

var test ={1:12}

是否存在于item_unit_associative_array

最佳答案

我认为您需要嵌套两层对象。结构见注释:

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]);
}
  

如果您不喜欢嵌套以及它使测试复杂化的方式,您还可以“字符串化”项目单元 ID 组合:

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

// Create the object like so:
// { 
//   "1:12": true,
//   "1:13": true, // etc.
// }
var map = items.reduce(function(map, itemId, i) {
  var id = itemId + ":" + units[i];
  map[id] = true;
  return map;
}, {});

console.log("1:13", map["1:13"]);
console.log("1:10", map["1:10"]);
console.log("2:10", map["2:10"]);

关于javascript - 在 JavaScript 数组中存储相同的键和不同的值并与键值对匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41123397/

相关文章:

javascript - 适合 100% 窗口大小的 Div 框架

在C中组合两个字符串

python - 将 ndarray 的最后一个维度转换为元组/重新排列

arrays - Bash 多个命令行参数

javascript - 如何在 Angular 7 中将 Ionic Style 属性与条件绑定(bind)?

javascript - 可折叠面板扩展器 : Can not get object in onLoad of page

javascript - 通过ajax检索文档后添加日期选择器

javascript - 为什么 Plupload 中的 'add files' 按钮不会在最新的 Chrome 或 OS X 上的 FF 中触发?

javascript - 为什么 onClick 事件在 html 中只工作一次?

javascript - 如何用 Jquery/JS 交换 2 @Html.DropDownListFor