3
     $(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););

From item_array and unit_array I want to build new object like

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

and finally want to check an object consists of key:value like

var test ={1:12}

is exists or not in the item_unit_associative_array

2
  • The best way to build a key/value pair grouping in JavaScript is with an object {1:12, 1:13 ,1:14,2:10} Commented Dec 13, 2016 at 14:23
  • 1
    What you're trying to do is not possible. Firstly what you have is syntactically wrong for an array, you need an object to store key/value pairs. However even then it wouldn't work as keys must be unique in an object. JS doesn't throw an error, but only the last value defined for the key will be stored. The closest thing you could do would be to store an array of objects, like this: [{ 1: 12 },{ 1: 13 },{ 1: 14 },{ 2: 10 }], or an object which holds arrays: { 1: [12, 13, 14], 2: [10] } Commented Dec 13, 2016 at 14:25

1 Answer 1

1

I think you need to nest two layers of objects. See the comments for the structure:

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

If you don't like the nesting and the way it complicates your test, you can also "stringify" the item-unit id combination:

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.