I am trying to remove 2 elements from end using splice method
This is what I am tryed
const arr = [3,4,5,6,7];
arr.splice(-1, 2)
I am expecting arr to be [3,4,5] but arr value is [3,4,5,6]
wanted to understand why it's behaving like this
From MDN
start
The index at which to start changing the array. If greater than the
length of the array, start will be set to the length of the array. If
negative, it will begin that many elements from the end of the array.
(In this case, the origin -1, meaning -n is the index of the nth last
element, and is therefore equivalent to the index of array.length -
n.) If array.length + start is less than 0, it will begin from index
0.
index -1 means from the last element so in our case, only the last element will be deleted.
You just start by the last index of the array and remove two items.
The result is an array without 7.
// -1 index
const arr = [3, 4, 5, 6, 7];
arr.splice(-1, 2);
console.log(arr);
The easiest way to do this will be to use arr.length
const arr = [3,4,5,6,7];
arr.splice(arr.length - 2)
console.log(arr)
First param tells to splice from which index it should delete all elements
const arr = [3,4,5,6,7];
const itemsToDelete = 2;
arr.splice(arr.length - itemsToDelete, itemsToDelete)
console.log('arr', arr);
Related
Array.prototype.push takes a value and appends it to the referenced array, returning its new length. Is it possible to instead get the index of an item when I append it?
You can subtract 1 from the length after adding the item to get its index:
const addIndex = (array, item) => array.push(item) - 1
const array = [1, 2, 3, 4, 5]
addIndex(array, 6)
//=> 5
I'm trying to write a function decreasingOrder which takes a positive integer as input and return an array of its digits in decreasing order.
e.g., decreasingOrder(1234) Should give [4,3,2,1].
function decreasingOrder(n) {
let unarr = [...`${n}`].map(i => parseInt(i)); //Unordered Array of Digits
let oarr = []; //Ordered Array of Digits
for(let j=0; j<unarr.length; j++){
let max = Math.max.apply(Math, unarr);
oarr.push(max);
unarr.splice(unarr.indexOf(max), 1); //delete element from array
}
return oarr;
}
console.log(decreasingOrder(1234));
//Expected [4,3,2,1], Instead got [4,3]
I think, deleting element using splice method also reduces the number
of iteration.
I also tried delete operator but get [4, NaN, NaN, NaN] (because Math.max([undefined])).
When I tried with specific number instead of unarr.length in condition expression for for loop, it works fine!
So when I use splice method to delete elements it reduces the unarr.length and when I tried to keep unarr.length constant using delete operator it gives NaN, what should I do? Is there any other way to write to the same function? I'm beginner in JavaScript.
The issue in your code is unarr.splice(unarr.indexOf(max), 1) inside loop.
By taking your example of console.log(decreasingOrder(1234)). In the first cycle the highest number from the array is found and is removed from the array and pushed to new array.
At the end of the first cycle the outputs will be unarr = [1, 2, 3], oarr = [4] and j=1
Likewise after second loop unarr = [1, 2], oarr = [4, 3] and j=2. Now the loop condition j < unarr.length is not satisfied hence the loop breaks. So the output will be [4, 3].
Instead you can use the below utility for your requirement.
function decreasingOrder(n) {
let unarr = [...`${n}`].map(i => parseInt(i)) //Unordered Array of Digits
return unarr.sort((a,b) => b-a)
}
console.log(decreasingOrder(1234))
Hope this helps.
I have an array:
let a = {"data": [1,2,4]};
I have tried to delete two element at the same time:
a.data.splice(0, 1);
a.data.splice(2, 1);
But second deling can not find index 2 bacause splice rebuilds indexes. How to fix it?
You could start from the end to beginning of the array. In short start with the larger index. This keeps the the index pointing to the same element.
let a = { data: [1, 2, 4] };
a.data.splice(2, 1);
a.data.splice(0, 1);
console.log(a);
After removing first element the remaining element will shift 1 index up. So you could try:
let a = {"data": [1,2,4]};
a.data.splice(0, 1);// after this the data will have [2, 4]
a.data.splice(1, 1);
console.log(a);
If you want to remove the first and last elements of an array, you can use respectively shift() and pop() :
let a = {"data": [1,2,4]};
a.data.shift();
a.data.pop();
console.log(a);
I have an integer array which looks like this:
var _SingleScannedItemIds = [];
At some point I push some items inside the array itself like following:
_SingleScannedItemIds.push(checkbox_value);
Now my question here is:
How do I know if the array contains anything inside of it?
How do I remove integer numbers from it, based on which parameter?
Can someone help me out? I'm using jQuery...
How do I know if the array contains anything inside of it?
if(_SingleScannedItemIds.length == 0)
How do I remove integer numbers from it, based on which parameter?
_SingleScannedItemIds.splice(pos, 1)
What the above does is it removes 1 element from the position pos.
EDIT
delete _SingleScannedItemIds[pos]
Makes the _SingleScannedItemIds[pos] = undefined and then you can reassign it later with ex. _SingleScannedItemIds[0] = "apple". assuming pos = 0
jQuery Has nothing to do here.
Just use array.length array.splice()
Splice(index, numberOfElementsToRemove)
To get the length of an array:
var array = [0, 1, 2, 4];
console.log(array.length);
Removing items from an array is done by the key:
function remove(array, element) {
const index = array.indexOf(element);
array.splice(index, 1);
}
var array = [0, 1, 2, 4];
remove(array, 1); //removes number 1
console.log(array);
I have an Array like
var myArray = new Array;
I have to push some elements to array in such a way that the elements will be replaced with same index.
Example :
myArray.push(1);
myArray.push(2);
myArray.push(3);
so now
myArray[0] = 1
myArray[1] = 2
now when i will push element 3 then
myArray[0] will be replaced with 3 and myArray[1] will be replaced with 1 and the element 2 will be removed.
It will continue according to the number of elements pushed...
Can any body help me with this requirement...
push adds to the end of an array. If you want to add a value to the beginning of an array you can use unshift.
myArray.unshift(3);
You can then use pop to remove the last element:
arr.pop();
DEMO
However, what you might need, given that you need to remove the same number of elements from an array that you add is a function that uses concat and slice instead:
function pusher(arr, add) {
return add.concat(arr).slice(0, arr.length);
}
var arr = [1, 2, 3, 4];
var arr = pusher(arr, [5, 6]); // [5, 6, 1, 2]
DEMO
I think you need something in the lines of:
myArray.unshift(element);
myArray.pop();
Explanation:
unshift: inserts the element on position 0 and moves all other elements one position to the right
pop: removes last element from array