0

I have a dictionary object in jQuery that contains data like this:

var category = [];
var brand = [];
var parameter = [];

category = $("input:checkbox[name=category]:checked").map(function() {
    return this.value;
}).get();

brand = $("input[type='radio']:checked").map(function() {
    return this.value;
}).get();

parameter.push({
    brand: brand,
    category: category,
});

Then I tried to remove the key and value pair from parameter dictonary like this but nothing got removed:

var brand = "something"
delete parameter.brand
console.log(parameter)

the parameter dictionary looks like this

Array[2]
0: Object
brand: Array[1]
0: "spykar"
length: 1
__proto__: Array[0]
category: Array[1]
0: "Men Jeans"
length: 1
__proto__: Array[0]
__proto__: Object
1: Object
brand: Array[1]
0: "Madame"
length: 1
__proto__: Array[0]
category: Array[1]
__proto__: Object
length: 2
__proto__: Array[0]

edit

As per Zamuka told me i tried to filter the array like this

$table.on('click', '.remove', function() {
              var brand = $(this).closest('td').prev('td').prev('td').html();
              console.log(brand)
              $(this).closest('tr').remove();
              parameter = parameter.filter(isItemOk);

              function isItemOk(item) {
               return item.brand != brand;
             }

            console.log(parameter)
        });

How can I delete a particular brand and corresponding categories from the parameter dictionary which contains more than one key value pairs of brand and categories?

asing177
  • 934
  • 2
  • 13
  • 34
  • 1
    `delete parameter[0].brand` – Rajaprabhu Aravindasamy Feb 26 '16 at 13:02
  • 1
    Possible duplicate of [javascript find and remove object in array based on key value](http://stackoverflow.com/questions/21659888/javascript-find-and-remove-object-in-array-based-on-key-value) – JJJ Feb 26 '16 at 13:10
  • parameter[0] deletes only the first one...i have multiple key value pairs of brand and categories@RajaprabhuAravindasamy – asing177 Feb 26 '16 at 13:17

2 Answers2

1

If you want to remove an item from array, first of all, you should get the index of this element. Then you can call delete, but it actually will replace array element with undefined. To completely remove an element, use splice:

var arr = ['qwe', 'rty']
var i = arr.indexOf('qwe')
arr.splice(i, 1); //['qwe']
console.log(arr); //['rty']

UPDATE

You can't delete from hash by value. You should find the key corresponding to value:

parameter = { brand: 'SomeBrand', category: 'Any' }
var brand = "SomeBrand"

for (var key in parameter) {
  if (parameter.hasOwnProperty(key) && parameter[key] === brand) {
    delete parameter[key]
  }
}

console.log(parameter); //Object {category: "Any"}
Alex Todef
  • 370
  • 2
  • 7
1

describe what items you want to keep and filter your array

// if brand is string
function isItemOk(item) {
    return item.brand != 'something';
}

parameter = parameter.filter(isItemOk);

or

// if brand is array _that contains_ a string
function isItemOk(item) {
    return item.brand.indexOf('something')==-1;
}

or same code a bit shorter :

parameter = parameter.filter((item)=>item.brand!='something');
zamuka
  • 796
  • 2
  • 9
  • 21
  • ahh.. forgot that jquery gives you not an array but array-like object. Use .map( someFunctionHere ).toArray() – zamuka Feb 26 '16 at 14:46
  • See, everything should be fine with parameter variable cause its an array you've made. I've missed that your brand property is also an array (why for if its a radio button). whatever.. just be creative with isItemOk function. function isItemOk(item) { return item.brand.indexOf('something')>-1; } PS: to make a fine string from an object avoiding __proto__ stuff - use JSON.stringify(parameter, null, 4); – zamuka Feb 26 '16 at 15:05
  • yeah actually i wrote that brand thing by mistake...it shudnt be an array – asing177 Feb 27 '16 at 09:02