array splice and console.log [duplicate] - javascript

This question already has an answer here:
Array is showing up as empty, yet it contains values [duplicate]
(1 answer)
Closed 5 years ago.
When I perform operations on an array in javascript, console.log shows the array as having been already changed BEFORE I do an operation on the array. I can reproduce this in chrome and firefox. Anyone know why this is?
var myTabs = [[0,0,0],
[0,0,0],
[0,0,0],
[0,0,0]];
console.log(myTabs);
myTabs[0].splice(1, 1);
console.log(myTabs);
See this for code:
https://jsfiddle.net/mxrh33t0/1/

When you expand a logged object in Chrome, you are expanding the last reference to it, not a copy of that object at the moment that it was logged.
In your case, the last reference was to the array after the splice() method had been called. If you would like to verify the difference, you will have to be more specific with your logging:
var myTabs = [[0,0,0],
[0,0,0],
[0,0,0],
[0,0,0]];
console.log(myTabs[0].join(","));
myTabs[0].splice(1, 1);
console.log(myTabs[0].join(","));
You can expand on that if you really want to see more.

Related

Filter Out Element from Nested Array [duplicate]

This question already has answers here:
Remove all falsy values from an array
(26 answers)
How to remove false values from array?
(6 answers)
Closed 3 days ago.
let input array=[["1.81","2.24"],["5.62","6.26"],false,["2.31","1.64"],false,false]
let output array=[["1.81","2.24"],["5.62","6.26"],["2.31","1.64"]];
I have a nested input array which contains smaller arrays and false statement as shown in the console. How do I remove the false statement from the input array? I have tried using for loop to loop through all the 6 elements to check each element with a (if !==false), then push into a new array called the output array but I could not get it to work? May I know how to solve this? Your help will be very much appreciated :)
Directly use Array#filter:
let input=[["1.81","2.24"],["5.62","6.26"],false,["2.31","1.64"],false,false]
let res = input.filter(Boolean);
console.log(res);

why is [[],[],[]] different than new Array(3).fill([])? [duplicate]

This question already has an answer here:
If we create an array of objects using new Array(len).fil({}); and then add a key in any of the objects, it gets reflected in all the 3 objects
(1 answer)
Closed 4 years ago.
So my question is the same as in the title. I saw that someone initiated a similar thing here (If we create an array of objects using new Array(len).fil({}); and then add a key in any of the objects, it gets reflected in all the 3 objects) but while everyone accused the improper method of questioning nobody gave a clear answer.
Array(3).fill([]) creates 3 elements referencing the passed object.
The answer is clear in javascript documentation:
The fill() method fills all the elements of an array from a start
index to an end index with a static value
It seems to me like the only answer in the question you linked to is perfectly clear.
Array.fill "fills" the array with static values. That is to say, you are creating on single empty array and then referencing it in all three of the locations, hence it is the same array in each of the outer array's indices

What is the difference between those two join usage? [duplicate]

This question already has an answer here:
join method not working on array JAVASCRIPT
(1 answer)
Closed 4 years ago.
Could anyone simply explain why the first method doesnt work as expected but inside console.log works perfect?
Simply I expected that It needs to return string but returns array in the first console
var elements = ['Fire', 'Wind', 'Rain'];
elements.join(''); //why that one is not working?
console.log(elements);
// expected output: Fire,Wind,Rain
console.log(elements.join(''));
// expected output: FireWindRain
You need to assign elements.join(''); to a variable:
var elements = ['Fire', 'Wind', 'Rain'];
elements = elements.join('');
console.log(elements);

Array.prototype.fill intended behavior on reference types [duplicate]

This question already has an answer here:
Unexpected behavior using Array Map on an Array Initialized with Array Fill [duplicate]
(1 answer)
Closed 6 years ago.
I know that this is the future technology, but MDN doesn't cover all use-cases, ans almost any environment already supports this method. Here's an example I am worried about:
var groups = new Array(4);
groups.fill([]);
groups[0].push(1);
groups[0] // --> [ 1 ]
groups[1] // --> [ 1 ]
So it's not just fills groups with empty array, it fills groups with the same one empty array!
Is this an intended behavior or things can change in the future?
It is the intended behavior according to the spec. The value is not cloned.

Delete Javascript object item adding null value [duplicate]

This question already has answers here:
Deleting array elements in JavaScript - delete vs splice
(29 answers)
Closed 8 years ago.
I have an javscript object
finalTitleList =[{"Title":"ffd","Iscompleted":"","Id":0},
{"Title":"fdfmdbk","Iscompleted":"","Id":1},
{"Title":"fdf,d","Iscompleted":"","Id":2}]
Suppose i like to delete an 2nd item using delete finalTitleList[1], after deletion it delete the item but length is not updated(snapshot attached: contain only 2 item but showing length 3).
So when i am adding that object in localstorage using
localStorage.setItem("TaskTitleList16", JSON.stringify(finalTitleList));
On place of deleteditem it shows null.
I want to completely remove that item, please help me.
You're wanting Array.prototype.splice().
This is an example of usage:
var a = [1, 2, 3, 4, 5];
a.splice(2,1); // Being splice at element 2, deleting (and returning) 1 element
console.log(a); // outputs [1, 2, 4, 5]
Delete works a little differently than you may expect with arrays, and shouldn't really be used. For one, delete doesn't affect the length of the array. For two, it leaves this undefined value in the middle of the array, which means you'll have to deal with it in code using the array.
In summary, don't use delete on arrays. There's a ton of built-in methods that you can use to work with arrays, or compose together into your own operations. Use them.

Categories

Resources