Slice a json array slice will not work

When i use slice to remove the first array I keep getting.

 TypeError: data.slice is not a function
    http://localhost/dev/ajax/error.html

My json data is as follow. I’m trying to remove "success:true

 {"success":true,"errOne":"Message One","#errTwo":"Message Two","#errThree":"Message Three"}

My jQuery code

 dataType:"json",
    cache:false,
    success: function(data){
    $('#errors div').empty();
    data.slice(1);

Any idea what I’m doing wrong and how to correct this?

You’re expecting that the data object will have a method called slice, which is wrong. The slice method only works on arrays that use numerically indexed values.

If you want to remove a property from the object, you can use the delete method.


delete data['success'];

Looking at your data though, I would reorganise it so that it has a separate success and failure object, to contain the data.


{
    'success': true,
    'failure': [
        { id: '#errOne', message: 'Message One' },
        { id: '#errTwo', message: 'Message Two' },
        { id: '#errThree', message: 'Message Three' }
    ]
}

That will make it a whole lot easier for you to process the data.

Thanks again, Paul. Delete does the job for now. I’m not alone on this and the other want it that way since its easier to generate those error messages. So be it :slight_smile: