Delete data from json array

后端 未结 3 1131
故里飘歌
故里飘歌 2021-02-15 00:11

I am trying to remove a piece of a data from a json array. For example I have this array

var favorites =    {
        \"userID\": \"12345678\",
        \"favorit         


        
3条回答
  •  梦如初夏
    2021-02-15 01:01

    I would most likely built a prototype method for this that makes the command a bit more simple to use

    // Place anywhere
    Object.prototype.cut = function(start, elements){
        return this.items.splice(start, elements);
    }
    
    // Call using this
    favorites.favorites[1].cut(1,1);
    

    This way you can extend methods and play around with the data in a very flexible way.

    == EDIT ==

    Maybe it was to flexible as Blue Skies pointed out. Updated example below. My style would be to add the favorites json to an object literal and include the methods you need in the literal. This example consists of the

    • JSON Data
    • a method to cut elements based on index
    • a method to get a favorite based on index
    • a method to return a favorite based on parameter name-value

    Snippet

    var favorites = {
        data: {
            "userID": "12345678",
            "favorites": [{
                "name": "My Favorites",
                "id": "87654321",
                "items": [{
                    "productID": "11234567",
                    "added": "TIMESTAMP",
                    "title": "Project",
                    "type": "Weekend Project",
                    "imageURL": "1"
                }, {
                    "productID": "11223456",
                    "added": "TIMESTAMP",
                    "title": "Bathroom",
                    "type": "Weekend Project",
                    "imageURL": "2"
                }, {
                    "productID": "11223345",
                    "added": "TIMESTAMP",
                    "title": "Curves",
                    "type": "Collections",
                    "imageURL": "3"
                }]
            }, {
                "name": "Bathroom",
                "id": "87654323",
                "items": [{
                    "productID": "11122224",
                    "added": "TIMESTAMP",
                    "title": "Project",
                    "type": "Weekend Project",
                    "imageURL": "1"
                }, {
                    "productID": "11122222",
                    "added": "TIMESTAMP",
                    "title": "Room",
                    "type": "Weekend Project",
                    "imageURL": "2"
                }, {
                    "productID": "11112222",
                    "added": "TIMESTAMP",
                    "title": "Strais",
                    "type": "Collections",
                    "imageURL": "3"
                },
    
                {
                    "productID": "11111222",
                    "added": "TIMESTAMP",
                    "title": "Door",
                    "type": "Collections",
                    "imageURL": "4"
                }]
            }]
        },
        cut: function(favorite, start, elements) {
            return this.data.favorites[favorite].items.splice(start, elements);
        },
        get: function(favorite) {
            return this.data.favorites[favorite];
        },
        find: function(value, param) {
            var found;
            this.data.favorites.filter(function(item, i) {
                if (item[param] === value) {
                    found = item;
                    return;
                };
            })
            return found;
        }
    };
    

    To use the find simply do something like this

    favorites.find("Bathroom", "name")
    

提交回复
热议问题