I understand that doing a normal array in javascript can i push and remove by doing this:
var array = ["a", "b", "c"];
var id = $(this).attr("id");
var index = $.inArray(id, array);
if(index === -1){
array.push(id);
}else{
array.splice(index, 1);
}
but what if i have an array with objects, i can push new items, but how to remove it?:
var array = ["a", Object{ qty="1" },
"b", Object{ qty="1" },
"c", Object{ qty="2" } ];
var id = $(this).attr("id");
var qty = $('#item_'+id).val();
var index = $.inArray(id, array);
if(index === -1){
array.push(id, {'qty' : qty});
}else{
array.splice(index, 1);
}
1 Answer 1
Assuming that "a", "b", and such can appear only once, you may not want an array at all, but rather some kind of "map" or "dictionary." In ES5 and earlier, it was common to use objects as maps; in ES2015 (aka ES6) and alter, Map is probably the new way to do it.
Since ES2015 is so new, here's the ES5 and earlier approach:
var map = {
"a": {qty: 1},
"b": {qty: 1},
"c": {qty: 2}
];
Each property, a, b, and c maps to an object with a property qty. (Presumably qty isn't the only property those objects will have; if it is, it would probably make more sense to use the value directly rather than wrapping it in an object.)
To add to this, you simply do assignment (rather than push) using the name of the new property, using either dot notation and a literal (unlikely, in your scenario I think) or brackets notation and a string (more likely):
var name = "d";
map[name] = {qty: 1};
To remove, you use the delete keyword rather than splice:
delete map[name];
Accessing is also via map[name]:
var name = "c";
var entry = map[name];
console.log(entry.qty); // 2
I mentioned ES2015's Map (MDN reference) above, so:
Creating:
var map = new Map();
map.set("a", {qty: 1});
map.set("b", {qty: 1});
map.set("c", {qty: 2});
or pass in an array of arrays, where the inner arrays have two entries: Name and value:
var map = new Map([
["a", {qty: 1}],
["b", {qty: 1}],
["c", {qty: 2}]
]);
You've already seen above how to add/update an entry:
var name = "d";
map.set(name, {qty: 1});
Removing:
var name = "b";
map.delete(name, {qty: 1});
Getting an entry:
var name = "c";
var entry = map.get(name);
console.log(entry.qty); // 2
Some advantages to Map:
The key can be anything, not just a string.
You don't have to worry about collision with property names from the prototype such as
toString.There's a "weak" variety that only weakly references its keys, meaning you can use keys that may get garbage collected even though they're in the map.
5 Comments
Object.keys(yourObject) to get an array of its property names (keys). If using a Map, you'd use yourMap.keys().Uncaught SyntaxError: Unexpected token [ for for(var [key, value] of map.entries()){} is this supported by chrome? if not how can i substitute this for loop ? ThanksmyMap.forEach(function(value, key) { console.log(key + " = " + value); }, myMap); Thanks a lot !
"a"appear more than once?array.splice(index, 2);, though using an object instead of an array might be even better.