How to remove an element from the array by index (Javascript)?

Asked

Viewed 123 times

0

I have the following array in Javascript:

var atr = [];
atr['font-size'] = '13px';
atr['font-weight'] = '800';

How do I remove from the array the element that has the key/index 'font-Weight'?

  • 6

    delete atr['font-weight']

  • Oxe, what do you mean this is an "Array"? By chance it is possible to create objects with [ ] ? As far as I know, arrays have no keys, someone explains it to me ?

  • 1

    @Jeanextreme002, in JS arrays are just objects that implement some methods for handling and iterating over their values. Everything you can do with an object you can do with an array as well. Of course, just because you can not mean you should.

  • @user140828 so arrays are children of objects let’s say so ?

  • @Jeanextreme002, yes, the class Array extends the class Object.

  • 1

    @Jeanextreme002 the construct [] creates a new array object is syntactic sugar for new Array(). Ex:https://repl.it/repls/ChartreuseTightRar . For indexes, in javascript the object Array can only have numerical indexes the indexes string are actually property of the object. Eg: https://repl.it/repls/PracticalBetterOmnipage

  • @Jeanextreme002 As already said, [] creates an array, but arrays are also objects and may have properties added to it. What might be confusing is that the syntax for creating properties also uses square brackets, which is the same mechanism for accessing the array indexes (see: https://repl.it/repls/FlimsyDangerousDiscussions) - it’s actually a little more complicated than this: https://answall.com/q/40682/112052

  • @Augustovasques In fact even numeric indexes are converted into strings - see the links in my previous comment. This one is very interesting: https://stackoverflow.com/q/27537677 - and finally: https://repl.it/repls/HandsomeVirtualArchitects

  • @hkotsubo, Documentation https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array is written: "Arrays cannot use strings as indexes (as in an associative array), integers must be used. Defining or accessing non-integers using bracket notation (or point notation) will not define or retrieve an element of the array itself, but rather define or access a variable associated with the collection of object properties of that array."

  • Javascript has a thing called implicit conversion when it does the ["1"] automatically converts the index.

  • 1

    @Augustovasques Yeah, reading calmly understood better (mainly the final part of this answer). If the key (whether number or string) represents a value between 0 and 4294967294, it is considered a numerical index of the array. Otherwise, it becomes a property (string). And "whatever" the browser engine handles internally (although the language specification hints that in the background everything turns into a string, but I may have got it wrong too)...

  • @hkotsubo I just wanted to say that I tried replicating the example https://repl.it/repls/HandsomeVirtualArchitects in Windows Script Host and failed miserably. I can’t adapt it here for(let i in v) {
 WScript.Echo( typeof i);
}
 for Jscript syntax.

Show 7 more comments

1 answer

0

The vector in javascript is an object, and to remove a field from a javascript object, you use delete.

So if I have:

var obj = {
   campo1: 'blabla', 
   campo2: 'blabla'
}

When I do: delete obj['campo1']; that field is erased! Leaving the obj with only the "field2".

For you to do this with a vector with non-numeric indexes is the same thing (since a vector is also an object). ````delete atr['font-size'];

The function indexOf(elemento) returns the index of an array where the indices are numerical! It does not work when the index an array is not a number.

This was said in the comments of the question as well. But I thought it best to leave in a reply.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.