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.
Related
This question already has answers here:
How do I check if an array includes a value in JavaScript?
(60 answers)
Remove duplicate values from JS array [duplicate]
(54 answers)
Closed 5 months ago.
I have an array with duplicate elements. While trying to remove duplicate elements using hasOwnProperty getting one duplicate element in array rest of duplicate element removed successfully. expexted output = [1, 3, 2, 4, 5, 6, 7] but getting something [1, 3, 2, 3, 4, 5, 6, 7]. I can use different function and remove duplicates but I'm not understanding why element 3 is coming twice.
var array = [1,3,2,1,3,4,5,6,7,3,5,6,4,3]
let output = []
function removeDuplicates(array){
for(let item of array){
if(!output.hasOwnProperty(item))
output.push(item)
}
return output
}
console.log(removeDuplicates(array));
Instead of using hasOwnProperty, you can use includes.
var array = [1,3,2,1,3,4,5,6,7,3,5,6,4,3]
let output = []
function removeDuplicates(array){
for(let item of array){
if(!output.includes(item))
output.push(item)
}
return output
}
console.log(removeDuplicates(array));
hasOwnProperty checks whether an object contains a given key. The correct way to check if an array contains an element is to use includes:
if(!output.includes(item))
output.push(item)
This question already has answers here:
How to get subarray from array?
(5 answers)
Closed 2 years ago.
I've searched up this question, and everywhere people seem to recommend to use array.splice(). However, splice is inplace, and, for example, in my javascript console editor.
Everywhere I seem to search, people say that splice does NOT mutate the original array, but that is clearly not the case. Now, I'm sure I will find another way to do what I want, but what is the proper way to make a copy of a piece of an array without affecting the original array?
You can use slice(), see below:
let x = [1, 2, 3, 4, 5]
console.log(x);
let sliced = x.slice(0, 2);
console.log(x);
console.log(sliced);
The slice() method returns a shallow copy of a portion of an array into a new array object selected from begin to end (end not included) where begin and end represent the index of items in that array. The original array will not be modified.
Make a copy of the array using the spread operator and then you can use splice or whatever.
let arr = [1, 2, 3, 4, 5];
let newArr = [...arr];
console.log(newArr);
// newArr.splice(......)
This question already has answers here:
Why Array.indexOf doesn't find identical looking objects
(8 answers)
Closed 3 years ago.
I'm wondering what the best way to determine the membership of one array in another array in JS.
Here's an example
let a = [];
a.push([1,2]);
a.includes([1,2]) <- evaluates to false
a.indexOf([1,2]) <- evaluates to -1
What's the deal here? Any efficient work around?
At the moment, your search array doesn't actually equal the array within your a array as they have 2 different references in memory. However, you could convert your arrays to strings, such that your search can equal another string array within your array.
To do this you could convert your inner arrays to string using .map(JSON.stringify) and then search for the string version of your array using .includes(JSON.stringify(search_arrr)).
See example below:
let a = [];
let search = [1, 2];
a.push([1,2]);
a = a.map(JSON.stringify)
console.log(a.includes(JSON.stringify(search)));
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.
This question already has answers here:
Picking 2 random elements from array
(10 answers)
Closed 7 years ago.
I have an array such as:
array=['a','b','c','d','e','f'];
I want to delete a random 2 elements. How can I do this?
To get two unique items from the array, and if you don't mind mutating the original array, you can use splice() to remove the selected item from the array so it won't be picked when you run it a second time:
var firstRandomChoice = array.splice(Math.floor(Math.random()*array.length), 1);
var secondRandomChoice = array.splice(Math.floor(Math.random()*array.length), 1);
If you use a utility library such as lodash, you may already have a function available to do this for you. For example, lodash provides sample(). So if you were using lodash, you could just do something like this to get an array of two random items:
var results = _.sample(array, 2);