JavaScript array delete Special [duplicate] - javascript

This question already has answers here:
Deleting array elements in JavaScript - delete vs splice
(29 answers)
Closed 9 years ago.
I have an array like:
var abc = ["a","b","c"];
And the indexes are 0,1,2
Suppose, I want to delete the 2nd item "b" and I the indexes swipe!
Out put:
abc = ["a","c"]
and the indexes are 0,1
How can I achieve this?

Use the splice function :
abc.splice(1,1) // from index 1, removes 1 element
Be careful that this changes the original array.

Use splice(). E.g:
abc.splice(1, 1);
would perform what you wanted in your example. abc[1] would now be "c".

You can use the array splice abc.splice(1,1);
Details:
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/splice

Have a look on it... I think this is what you want...
var arr = ["a","b","c"];
arr.splice(1,1);
alert("["+arr.indexOf('a')+","+arr.indexOf('c')+"]");

Related

Javascript: how to join two arrays [duplicate]

This question already has answers here:
Javascript Array Concat not working. Why?
(7 answers)
Closed 6 years ago.
I know this has been asked a lot of times, but I cannot get it to work.
I have an empty array a
var a = [];
and an array with an object b
var b = [{
title: 'test'
}]
I want to join them so a will look exactly like b.
The idea is to do this inside a for loop so a would be added a new item each time.
By using a.concat(b), a results in an empty array.
Not sure what I am missing.
Per Array.prototype.concat()
This method does not change the existing arrays, but instead returns a new array.
You need to assign this operation back to a
a = a.concat(b)
You need to assign result of that call to a. a = a.concat(b)

Finding specific objects in array to remove [duplicate]

This question already has answers here:
Best way to find if an item is in a JavaScript array? [duplicate]
(8 answers)
Closed 7 years ago.
I have an array of data which stores an object with functions and other such info. I push these objects to the function for my draw function to execute.
But i do not know of a way to find a specific object in an array to remove it and thus stop drawing it.
For example i have an array structure like this:
var data = {
'fnc':function(){
updatePosition(spriteID);
drawSprite(spriteID);
},
'something':'here'
};
var drawOrder= [];
drawOrder.push(data);
There are many functions in this array and they are pushed dynamically depending on what i wish to draw.
What would be the best way to find the index of one of these objects and remove it from the array in this case?
indexOf() returns the index in the array of the element you're searching for, or -1. So you can do:
var index = drawOrder.indexOf("aKey");
if (index != -1)
drawOrder.splice(index, 1);
Splice:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice
indexOf:
http://www.w3schools.com/jsref/jsref_indexof_array.asp
I'm not 100% this will answer your question cause is not clear at least to me.
If you want to remove the whole element but you are worried about founding the right index before actually splice the array you should use Array.indexOF
See this code below:
var data = {
'fnc':function(){
updatePosition(spriteID);
drawSprite(spriteID);
},
'type':'aabb'
};
var drawOrder= [];
drawOrder.push(data);
console.log(drawOrder);
drawOrder.splice(drawOrder.indexOf(data), 1);
console.log(drawOrder);
As the documentation reports:
The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.

I have my array as arr=[1,2,3,4]. I pushed an element in it arr.push(5). Now I need to display my array as [5,4,3,2,1]. Any Idea? [duplicate]

This question already has answers here:
How can I reverse an array in JavaScript without using libraries?
(36 answers)
Closed 7 years ago.
I have an array as:
var arr = [1,2,3,4]
//Push and element to array
arr.push(5)
//Now, arr = [1,2,3,4,5]
I need to display my array as
Elements in array arr is:
5,1,2,3,4
Arr.reverse() gives me
5,4,3,2,1.
But i need
5,1,2,3,4
Simply use Array.prototype.reverse():
console.log(arr.reverse());
References:
Array.prototype.reverse().
This is the code you need to use :
arr.reverse();
Here is the w3school : http://www.w3schools.com/jsref/jsref_reverse.asp

How to completely delete element from Javascript array [duplicate]

This question already has answers here:
How can I remove a specific item from an array in JavaScript?
(142 answers)
Closed 7 years ago.
I am using this code to iterate over an array and delete the element if it contains no itemsToPost.
rssFeeds.forEach(function(feed, index, array){
if(feed.itemsToPost.length == 0){
delete rssFeeds[index]
}
});
However, the array that I receive from this, assuming that all elements are deleted looks like:
[ , ]
And when I run this against Lodash's method to test if an array is empty.
console.log(_.isEmpty(array))) // returns 'false'
How can I completely delete these elements such that the array is truly empty?
Update:
I've found a solution using Array.prototype.filter as was suggested, such as:
function hasNewItems(array) {
return array.itemsToPost.length > 0;
}
var newArray = rssFeeds.filter(hasNewItems)
console.log(newArray); // returns []
It isn't necessarily deleting the array but it has the desired effect.
delete is not the correct way to remove element from array, you should use rssFeeds.splice(index, 1) instead.

Javascript - Modify an array duplicate without modifying the original array [duplicate]

This question already has answers here:
Why does changing an Array in JavaScript affect copies of the array?
(12 answers)
Closed 8 years ago.
I'm sure this is a stupid thing, but i can't solve it.
If i do this:
var arr = [1, 2, 3, "up"];
var duplicate = arr;
duplicate[3] = "down";
console.log(arr[3]); //"down"
console.log(duplicate[3]); //"down"
So why the original array got modified too? It is related to the fact that they point to the same array?
How to modify only the duplicate?
You need to clone your duplicate array... you can use the slice() method to do it:
var arr = [1, 2, 3, "up"];
var duplicate = arr.slice(0);
duplicate[3] = "down";
console.log(arr[3]); //"up"
console.log(duplicate[3]); //"down"
The original array was modified because using var duplicate = arr just means the value of duplicate is now equal to the value of arr. Changing either one will change the value of the array.
As for copying the array and its contents, this post has a full write up How do I correctly clone a JavaScript object?

Categories