如何在javascript中删除对象的一部分 [英] How to remove a part of an object in javascript

查看:69
本文介绍了如何在javascript中删除对象的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

var data = [];
$("#btn").click(function(){
    total++;
    data.push({        
        id : total,
        "cell": [
            "val1",
            "val2",
            "val3",
        ]
    });
});

每次用户点击 btn 按钮,我向数据对象添加一些值。现在我的问题在于我如何删除 id = X

Every time that user clicks on btn button, I add some values to the data object. Now my question is here that how I can remove the part that has id = X

推荐答案

你可以在X位使用.splice()

You can use .splice() at position X

var data = [{id : total, "cell" : ["val1", "val2", "val3"]}[, ...repeat];

var X = 0; // position to remove
data.splice(X,1);

分机:

for (var i=data.length-1; 0 < i; i--) {
    if (data[i].id == X) {
        data.splice(X,1);
        break;
    }
}

这篇关于如何在javascript中删除对象的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆