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);
}
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);
}
Share
Improve this question
edited Nov 16, 2015 at 16:44
thanksd
55.7k23 gold badges165 silver badges154 bronze badges
asked Nov 16, 2015 at 16:06
ndARndAR
3712 gold badges4 silver badges16 bronze badges
5
- 1 Apologies, but your question is a little unclear. In your second example with the objects, is that array suppose to contain 6 items, 3 letters and 3 objects or are you intending it to be more like a key-value pair where each letter is a key that corresponds to an acpanying object? – War10ck Commented Nov 16, 2015 at 16:09
- Maybe i write incorrectly the array, bat each a, b and c need to coordinate with each object. Thx – ndAR Commented Nov 16, 2015 at 16:10
-
Are the letters unique? Or can
"a"
appear more than once? – T.J. Crowder Commented Nov 16, 2015 at 16:12 -
1
I guess you want
array.splice(index, 2);
, though using an object instead of an array might be even better. – Felix Kling Commented Nov 16, 2015 at 16:15 - Sorry for the delay, yes, the key a b c .. are unique, so each key will contain object data, or maybe it can be a multidimensional array, but i cannot understand very well the structure. – ndAR Commented Nov 16, 2015 at 16:24
1 Answer
Reset to default 2Assuming 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 mon 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.