Delete value from array object in javascript (jquery) - javascript

I am trying to delete objects from my array via javascript. Im building a simple mobile jquery application and want to delete items from an array. I use the following code. I have a list with checkboxes in it. Every checkbox has a value that belongs to the list item. So when multiple checkboxes are checked.. it should delete all these objects (items) from the array.
function deleteFunction()
{
objects = getObjects();
$("input:checked").each(function()
{
var inputValue = $(this).val();
for(i = getObjects().length; i >=0; i--)
{
if('{"title":"'+ inputValue + '"}' == JSON.stringify(objects[i]))
{
objects.splice(i, 1);
return true;
}
}
});
alert(JSON.stringify(objects));
window.location.reload();
}
The annoying thing is as follows:
When i slice the object from the array, the object is restored on the second iteration. So it always removes only 1 object from the array.
To test my output i used the following code within the if statement:
alert(i);
alert(JSON.stringify(objects[i]));
objects.splice(i, 1);
alert(i);
alert(JSON.stringify(objects));
return true;
The output is as follows
1
{"title":"hi2"}
1
[{"title":"hi1"}, {"title":"hi3"}]
2
{"title":"hi3"}
2
[{"title":"hi1"}, {"title":"hi2"}]
so i slice hi2, but has returned
Thanx for answer and respond
Solution Thanx to depperm + indubitablee:
function deleteFunction()
{
var objects = getObjects();
$("input:checked").each(function()
{
var inputValue = $(this).val();
for(i = objects.length -1; i >=0; i--)
{
if(objects[i].title == inputValue)
{
objects.splice(i, 1);
}
}
});
localStorage.setItem("objects", JSON.stringify(objects));
window.location.reload();
}

There are a few things I would change, first in the for loop no need to call getObjects() each time just use objects. Then in the if simply check if the objects[i].title is the same as the inputValue.
function deleteFunction()
{
objects = getObjects();
$("input:checked").each(function()
{
var inputValue = $(this).val();
for(i = objects .length; i >=0; i--)
{
if(objects[i].title==inputValue)
{
objects.splice(i, 1);
}
}
});
alert(JSON.stringify(objects));
window.location.reload();
}

use .splice() instead of .slice().
slice does NOT alter/manipulate the original array at all, it just creates another array based on your selection.
splice does alter the original array.
reference: http://www.devcurry.com/2010/12/slice-and-splice-in-javascript.html

The array method slice does not remove array elements. Use splice instead.
There are some problems with your code:
You missed the keyword var sometimes.
The variable i is equal to the length of the array in the first iteration. It should be one minus that.
Instead of converting to JSON to compare the objects, you could have just compared the value of the title property.

Related

Using Javascript Array Filter method to apply logic [duplicate]

I have search through quite a lot of questions here, but havent found one that i think fits my bill, so if you know of one please link to it.
I have an array that i want to search through for a specific number and if that number is in the array, i then want to take an action and if not then another action.
I have something like this
var Array = ["1","8","17","14","11","20","2","6"];
for(x=0;x<=Array.length;x++)
{
if(Array[x]==8)
then change picture.src to srcpicture1
else
then change picture.src to srcpicture2
}
but this will run the lenght of the array and end up checking the last element of the array and since the last element is not 8 then it will change the picture to picture2.
Now i can see why this happens, i just dont have any ideas as to how to go about checking if an array contains a specific number.
Thanks in advance.
What you can do is write yourself a function to check if an element belongs to an array:
function inArray(array, value) {
for (var i = 0; i < array.length; i++) {
if (array[i] == value) return true;
}
return false;
}
And the just do:
var arr = ["1","8","17","14","11","20","2","6"];
if (inArray(arr, 8)) {
// change picture.src to srcpicture1
} else {
// change picture.src to srcpicture2
}
It's a lot more readable to me.
For extra points you can add the function to the array prototype like so:
Array.prototype.has = function (value) {
for (var i = 0; i < this.length; i++) {
if (this[i] === value) return true;
}
return false;
};
And then the call would be
if (arr.has(8)) // ...
Pushing this even further, you can check for indexOf() method on array and use it - if not - replace it with the code above.
P.S. Try not to use Array for a variable name, since it's reserved for the actual array type.
use this
http://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Objects/Array/IndexOf
ie version
https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Objects/Array/IndexOf#Compatibility
Why don't just you abort the loop when you find the right number :
for(x=0;x<=Array.length;x++)
{
if(Array[x]==8) {
//change picture.src to srcpicture1
break;
}
}
You could sort the array first then check the array only up to the point at which a number would be in the array, were it to exist.
If you have unique keys and a faster retrieval is what you care about a lot, you can consider using a map instead of an array (if there's a hard-bound case of using an array, then it won't work of course). If using a map, you just check "if( num in arr ) ".

How to compare the values of a particular key from both arrays using inArray in jQuery

I have two array with objects inside in it. I want to compare the values of a particular key from both arrays and do something with that. I have tried using inArray but couldn't succeed. Below is my code.
function initialize() {
geometry = 'http://yyyy.cartodb.com/api/v2/sql?q=SELECT name,ST_AsGeojson(the_geom) from msa_usa';
$.getJSON(geometry,
function(data) {
var place_names = [];
place_names.push({
name: "Abilene",
average: 8.65
});
for (i = 0; i < place_names.length; i++) {
if ($.inArray(data.rows[i].name, place_names[i].name) > -1) {
geom.push((data.rows[i].st_asgeojson));
average_value.push(place_names[i].average);
} else
(console.log("else"));
//console.log(place_names[i].name);
}
})
}
console.log(average_value.length);
console.log(geom.length);
I'm not sure why you're trying to use $.inArray() at all. It appears you're just trying to compare two properties on two objects (which happen to be in arrays, but you're already indexing to a particular object). If that's the case, then you can just directly compare the two properties:
if (data.rows[i].name === place_names[i].name)
But, in your code, you appear to have just created place_names so it will only have one value in it, not a whole bunch of values in it. So, now that confuses me as to what you're really trying to do here.
For more help, please describe in words what you're really trying to accomplish. Are you just trying to see if one particular .name property is in the data.rows array of objects?
If so, that would be a different piece of code like this:
function findPropInArray(array, propName, value) {
for (var i = 0; i < array.length; i++) {
if (array[i][propName] === value) {
return i;
}
}
return -1;
}
Then, in your code you could use something like this:
if (findPropInArray(data.rows, "name", "Abilene") !== -1) {
// "Abilene" was as a name property in data.rows
}

javascript : after calling the splice() control comes out from the loop

I have an array rosters and i want to alter this array according to some conditions. Here what I'm trying to do.
somefunction(callback) {
for (var i in this.rosters) {
var roster = this.rosters[i];
if (roster.age > 7200) {
this.rosters.splice(i, 1);
} else {
this.rosters[i].age = this.EMarshal.tu.getAgeOfTime(
this.EMarshal.tu.getMyTime(
this.EMarshal.tu.getMyDate(roster.date), roster.shifttime
)
);
console.log(this.rosters[i].age);
}
}
callback();
}
When the the if condition is true and splice is been called, control comes out of from loop and call callback(). But i want to run the loop for each values in the array.
plz carefully notice that there are rosters and roster 2 different variables.
Any idea why its happening and the solution will be usefull.
Thanks
It's just because you are trying to alter the array on which you are iterating.
So, just ad some logic to store the indexes as you have said you have tried.
Here is one suggestion
before getting into loop var index = [];
then your if condition
if (roster.age > 7200) {
index.push(i);
}
and then after the loop, remove those indexes from rosters
for (var j = index.length - 1; j > -1; j-- ) {
console.log(j);
this.rosters.splice(index[j], 1);
}
Remember to iterate the index from last index otherwise you will remove the 1st index and the trying to remove the last index from the rosters, but now you have removed the element from the array so the length is been changed.

javascript, remove identical items from array

I do an ajax call in a lightbox script which returns a form.
when the form is loaded I call var initial = $('form').serializeArray();
when the form is submitted I call var final = $('form').serializeArray();
which gives me two arrays of objects,
What I now want to do is compare each object in the arrays and remove those that have not changed.
how would I do this?
I'm assuming that the two arrays will have equal length, and that the elements will be in the same order in both arrays. In this case, what you need to do is look at each element of the first array and compare it to the corresponding element of the second array; if they match, then remove the element in that position from both arrays.
Something like this should work (though I haven't tested it):
var i = 0;
while (i < initial.length) {
if(initial[i] == final[i]) {
initial.splice(i,1);
final.splice(i,1);
}
else {
i++;
}
}
The fastest way to do this I think
var len = initial.length, i=0, changed=[];
/* I hope initial.length==final.length*/
for(i; i<len; i++){
/* 0== '' */
if (initial[i]===final[i])
changed[i] = final[i];
}
//now play with
changed
I've got confused abou the question
does .splice() reorder the indexes?

How to re-label Object in array?

Extending a previous question about JavaScript and jQuery, I'm trying to make an array and then look into it, but the array contains dimensions and I can't get it right..
var markers = [];
$.getJSON('GetList', "id"= + data.id,
function(data){
$.each(data.list, function(index, data) {
}
markers.push( {
category:data.category,
id:data.id,
location:{
latitude:data.location.latitude,
longitude:data.location.longitude
}
});
});
});
return markers;
}
The first thing that strikes me is that every item will now be called "Object", while default, I'm still wondering if they can be labeled instead?
Further down the line, I try to access the id to compare it to a selection by the user,
but it doesn't work.
var selection = $(div_id).val();
var arr = $.inArray(selection, markersArray.id);
if( arr >= 0) {
return search(selection, div_id);
} else {
throw("selection not found in selectionSearch()");
}
What am I doing wrong here..?
To label the objects, add a toString function, like this:
markers.push({
toString: function () { return this.something; },
category:data.category,
id:data.id,
location:{
latitude:data.location.latitude,
longitude:data.location.longitude
}
});
To search the array, you'll need your own loop.
jQuery's $.inArray function searches the array for an exact match; it cannot be used to find an object with a matching property.
You can find a match yourself, like this:
for(var i = 0; i < markers.length; i++) {
if (markers[i].id === selection)
return search(selection, div_id);
}
throw("selection not found in selectionSearch()");
The type of each item is Object, there is nothing strange about that. There is no reason to try to change that.
To filter out items from an array based on a property in the item, you use the grep method:
var arr = $.grep(markers, function(e){ return e.id === selection; });
To check if the array is empty, you don't compare the array to a number, you use the length property:
if (arr.length > 0) {

Categories

Resources