What is called this array ... and how to remove elements from it

I have an array that I use, I am having difficulty describing what kind of array it is, which makes it difficult to work with it. So far this works for me. I'm just curious.

Ultimately, I want to remove the end of this array.

I tried .pop() and .grep() . He does not work.

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

I am trying to do the following:

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

For clarification, I do not know the exact name of option_label .

+5
source share
3 answers

This is a Javascript object. You may need to look at this question to remove properties, this provides different ways. One of them:

 delete options.something; 
+1
source

What is this array called ...

This is not an array. This is an object. An array is an ordered set of records defined by an index value (number). An object (in JavaScript) is a collection 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 a squared syntax and a string expression for the property name (which can be a reference to a variable, for example, to your option_label ):

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

... or with the dotted syntax and property name literal:

 delete options.someLiteralPropertyName; 

.pop does not exist for objects, because objects have no order, so the concept of the "last" record of an object does not make sense.

+2
source

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

 delete options[option_label]; 
+1
source

Source: https://habr.com/ru/post/1203015/


All Articles