Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

I am creating objects when textbox having some values (using ng-blur and textbox.value!==undefined) and then putting these objects in an array (all working fine here).

When I click on checkbox (checkbox model bind with textbox ng-required) I need to delete that particular object having that textbox value. I am using:

arr.splice(index,1);

to remove that particular object from array (by matching it's name like "monthly" or "quarterly" etc.), but it is creating null at that particular position.

for e.g. [object,object,object]

[
{name:"monthly",
  amount:1000 },

{name:"quarterly",
  amount:1200 },

{name:"yearly",
  amount:1300 }
]

after removing all element it shows [] and when I add another new object it displays [3:object] and it's content as [null,null,null,object];

or

if I remove middle object say name:"quarterly", it shows [object,object] but after adding a new object it display array as [object,object,null,object] with length of array as 4.

Why is there null and how can I remove that from array. (don't want to iterate again to check null).

share|improve this question
    
Show us the code (plunkr preferably). Splice doesn't have that effect . –  sirrocco Aug 27 at 11:41
2  
How are you adding the new objects to the array? Would be the best if you add a snippet with all the relevant code. –  David Aug 27 at 11:41
    
are you using loadash or underscore? –  Preethi Aug 27 at 12:13
    
how are u adding element? –  Preethi Aug 27 at 12:25

2 Answers 2

It is difficult to say why your code creates the null values without have a look to it.

But I can say you that it is not the expected behaviour.

You can see this example to get some inspiration:

var data = [
{name:"monthly",
  amount:1000 },

{name:"quarterly",
  amount:1200 },

{name:"yearly",
  amount:1300 }
];

var newObjectToBeAdded = { name: "daily", amount:"100" }

function showObjects()
{
     document.body.innerHTML += data + '<hr>';
}

function deleteObjectByName( objectName )
{
    for( var i = 0; i < data.length; i++ )
    {
    	if( data[ i ].name == objectName )
        {
             data.splice(i, 1);
        }
    }
}

function addObjectToData( newObject )
{
    data.push( newObject );
}

showObjects();
deleteObjectByName( "quarterly" );
showObjects();
addObjectToData( newObjectToBeAdded );
showObjects();

Just to throw a guess out, maybe you are accidentally duplicating the array. Maybe in some point of your code you are doing something like this:

var new_array = original_array.splice( index );

Or creating the new array in the loop you use to find the target object, or using some kind of intermediate array, etc.

Hope it helps!

share|improve this answer
var arrayWithoutNulls = myArray.filter(function(val) {
    if (val) {
        return val;
    }
});
share|improve this answer
1  
If that was suitable, then the callback need only be: function(val){return !!val}. –  RobG Aug 27 at 12:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.