0

var array = new Array(5);
array[0] = 1;
array[1] = 1;
array[3] = 1;
array[4] = 1;
console.log(array.hasOwnProperty(0)); //true
console.log(array.hasOwnProperty(1)); //true
console.log(array.hasOwnProperty(2)); //false
console.log(array.hasOwnProperty(3)); //true
console.log(array.hasOwnProperty(4)); //true

I want to insert array that have unsetted element.

var array = [1,1]
var arrayToInsert = new Array(3);
arrayToInsert[0] = 1;
arrayToInsert[2] = 1;
array.splice(1, 0, ...arrayToInsert);
console.log(array.hasOwnProperty(0)); //true
console.log(array.hasOwnProperty(1)); //true
console.log(array.hasOwnProperty(2)); //true, I want to make it false.
console.log(array.hasOwnProperty(3)); //true
console.log(array.hasOwnProperty(4)); //true

How keep unsetted element after I splice(insert) array that have unsetted element?

asked Feb 14, 2020 at 13:56
13
  • 5
    There is no empty value in JavaScript. Commented Feb 14, 2020 at 13:58
  • 1
    The console output "empty" there is no actual empty value. That is just what the browser does when all the indexes are undefined. Commented Feb 14, 2020 at 13:59
  • 1
    @Cid That's a browser/snippet artifact; if you run it in the console it will display it as stated in the post. Commented Feb 14, 2020 at 14:00
  • 3
    Possibly, but even that smells of being an XY problem. Commented Feb 14, 2020 at 14:07
  • 2
    I think I was wrong. I thought it was important to keep the empty element. Commented Feb 14, 2020 at 14:49

2 Answers 2

1

Not sure to understand your question, but if you want to unset a value from your array (so, setting it as undefined), you can delete the array element:

const a = [1, 2, 3, 4];
console.log(a);
delete(a[2]);
console.log(a);

answered Feb 14, 2020 at 14:11
Sign up to request clarification or add additional context in comments.

3 Comments

Note that, depending on the browser, this still shows up as undefined on the console, and it will behave that way too with traditional for (var i = 0; i < a.length; i++) loops...
I want to insert array that have unsetted element. Isn't there way to automate delete unsetted elements?
@coloud There's no such thing as "inserting unsetted elements". You either insert elements or you don't. If you do insert elements, those elements have to be something. And they'll be undefined, which is the value for "nothing".
0

If you want to insert a value meaning "empty" into an array, simply assign the special value of undefined to your chosen position in the array.

var array = ['a','b', 'c', 'd', 'e']
var arrayToInsert = ['🍿', undefined, '⛱']
array.splice(1, 0, ...arrayToInsert)
console.log(array)
console.log(Object.keys(array))
console.log(array.length)

When you see the word "empty" written in the console, is used by Chrome to indicate the absence of a property corresponding to an index.

Note that the printing of the word "empty" to the console is a peculiarity of Chrome and is not part of the JavaScript standard.

To remove one of the index properties from an array, you use the delete keyword.

Note how doing so looks different in the consoles of Chrome, Safari and Firefox:

Chrome Safari Firefox

answered Feb 14, 2020 at 15:11

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.