SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    slice a json array slice will not work

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

    Code:
     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

    Code:
     {"success":true,"errOne":"Message One","#errTwo":"Message Two","#errThree":"Message Three"}
    My jQuery code

    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?

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Nordy View Post
    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.

    Code javascript:
    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.

    Code:
    {
        '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.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •