40

I have an array that I am using, I am having difficulties describing what kind of an array it is, which is making it hard for me to work with it. So far it works for me. I am just curious.

I eventually want to remove the end of this array.

I tried .pop() and .grep(). Its not working.

Here is an example of my code.

var options = {};
$('.option:visible').each(function(){
     var option_label = "";
     var option_selected = [];
     var option_img = "";

     ...


     options[option_label] = {
         option_selected: option_selected,
         option_image : option_img
     };
});

What I am trying to do is:

if(option_label.indexOf("something") != -1) {
   //then pop off options
} 
//continue about your business

For clarification I wont know the exact title of the option_label.

Aedonis
  • 611
  • 4
  • 11
  • `options` is an `object`. In JS, you can add properties to objects using `obj[propName]`. – Davin Tryon Sep 19 '14 at 22:35
  • 1
    Which variable are you referring to? `option_selected`? That's the only array I see. – slebetman Sep 19 '14 at 22:35
  • The only array that you have is `option_selected = []`, and that is already empty, so what are you trying to remove from it? – Guffa Sep 19 '14 at 22:35
  • 1
    "options" isn't an array. It's an object, which contains key-value pairs. You can't `pop()`object properties because they're not guaranteed to be in any set order. Therefore there is no "end" or "last" you can get rid of ...Looks like others beat me to linking the related question. – mc01 Sep 19 '14 at 22:37
  • 2
    He obviously didn't realise it was an object (hence the question "what is this array called"). therefore the "possible duplicate of" doesn't really make sense... – Cyril Duchon-Doris Sep 19 '14 at 22:41

3 Answers3

13

It is a Javascript Object. You might want ot have a look at this question to remove properties, it gives different ways to to that. One of them is :

delete options.something;
Community
  • 1
  • 1
Cyril Duchon-Doris
  • 12,964
  • 9
  • 77
  • 164
4

Basically an object (or you can view it as an associative array), so try delete:

delete  options[option_label];
Peter Pei Guo
  • 7,770
  • 18
  • 35
  • 54
3

What is this array called…

It's not an array at all. It's an object. An array is an ordered set of entries keyed by an index value (a number). An object (in JavaScript) is a set of unordered key/value pairs.

...and how to remove items from it

To remove a property from an object, you use delete, specifying the property either with bracketed syntax and a string expression for the property name (which can be a variable reference, for instance to your option_label):

delete options[option_label];
// or
delete options["some property name"];
// or
delete options["some " + " property" + "name"];

...or with dot syntax and a literal property name:

delete options.someLiteralPropertyName;

.pop doesn't exist for objects, because objects have no order, so the concept of popping the "last" entry off of an object is meaningless.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875