具有Array的Object删除元素行为

时间:2017-10-13 05:56:57

标签: javascript arrays

这里的问题, 我有一个Object包含几个元素

示例:

obj = {
    "0": { "a": "a1", "b": "b1"},
    "1": { "a": "a2", "b": "b2"}
};

现在,我希望删除/删除element 0,因此只剩下element 1,但不是从Object删除元素的预期结果,我会就像它像Array一样,第二个元素将替换第一个元素,通过上面的示例,1将重命名为0以及后续的所有元素。

注意:对象中可能包含2个以上的元素,并且不需要删除第一个元素,它可以是对象中的任何元素

这是我到目前为止所做的解决方案,

var index = 0 //just for demo purpose

var tempArr = Object.keys(obj).map(function(key){
    return obj[key];
});

tempArr.splice(index,1);

obj = tempArr.reduce(function(acc, cur, i){
    acc[i] = cur;
    return acc;
}, {});

结果:

obj = {
    "0": { "a": "a2", "b": "b2"}
}

这个解决方案有效,并给出了我想要的结果,但有没有更好的方法呢?

编辑:作为建议和讨论的结论

解决问题的替代方法,它提供与我的解决方案几乎相同的结果。

function Delete(index) {
    delete obj[index];
    var keys = Object.keys(obj);
    var update = false;
    for (var i = index + 1; i <= keys.length; i++) {
        obj[i - 1] = obj[i];
        update = true;
    }
    if (update) delete obj[keys[keys.length - 1]];
}
@Anthony McGrath

delete obj[index];
obj = Object.keys(obj).reduce(function(newobj,key,i){
    newobj[i]=obj[key];
    return newobj;
},{});
来自@ Alexandru-Ionut Mihai的

obj.length = Math.max.apply(null, Object.keys(obj))+1;
Array.prototype.splice.call(obj, index, 1);
delete obj.length; // optionally delete the length again

by @ t.niese

再次感谢你的帮助。

4 个答案:

答案 0 :(得分:1)

你可以做的一件事 - 如果它只有数字键 - 是通过添加splice然后在其上调用obj = { "0": { "a": "a1", "b": "b1"}, "1": { "a": "a2", "b": "b2"}, "2": { "a": "a3", "b": "b3"}, "3": { "a": "a4", "b": "b4"} }; var index = 1; obj.length = Math.max.apply(null, Object.keys(obj))+1; Array.prototype.splice.call(obj, index, 1); delete obj.length; // optionally delete the length again console.dir(obj)将其转换为类似对象的数组。

&#13;
&#13;
SELECT 
    CONVERT(VARCHAR(10), 
            CAST(SUM(DATEDIFF(Minute, startdate,EndDate) / 60) 
                OVER (PARTITION BY clientId ORDER BY clientId)) AS VARCHAR(2))
       + '.' 
       + RIGHT('00' + CONVERT(VARCHAR(2), 
            SUM(DATEDIFF(Minute, startdate, EndDate)) % 60), 2) 
                (OVER(PARTITION BY clientId ORDER BY clientId))                        
FROM tableX;
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您只能使用reduce方法执行此操作。

另外,使用delete运算符可以从object

中删除一个属性

&#13;
&#13;
obj = {
    "0": { "a": "a1", "b": "b1"},
    "1": { "a": "a2", "b": "b2"}
};
delete obj['0'];
newobj=Object.keys(obj).reduce(function(newobj,key,i){
    newobj[i]=obj[key];
    return newobj;
},{});
console.log(newobj);
&#13;
&#13;
&#13;

答案 2 :(得分:1)

这个怎么样?

 obj = {
    "0": { "a": "a1", "b": "b1"},
    "1": { "a": "a2", "b": "b2"}
};

function Delete(p) {
    delete obj[p];
    var keys = Object.keys(obj);
    var update = false;
    for (var i = p + 1; i <= keys.length; i++) {
        obj[keys[i] - 1] = obj[keys[i]];
        update = true;
    }
    if (update)
        delete obj[keys[keys.length - 1]];
}

Delete(0); // delete first element

答案 3 :(得分:0)

您可以获取所有索引,这些索引必须移动并为其提供新的索引/键。最后删除不需要的最后一个对象。

&#13;
&#13;
function deleteFromObject(object, index) {
    Object.keys(object).slice(index + 1).forEach(function (k) {
        object[index++] = object[k];
    });
    delete object[index];
}

var object = { 0: { a: "a1", b: "b1" }, 1: { a: "a2", b: "b2" }, 2: { a: "a3", b: "b3" }, 3: { a: "a4", b: "b4" } };

deleteFromObject(object, 0);
console.log(object);

deleteFromObject(object, 2);
console.log(object);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
&#13;
&#13;