在javascript中,如何从对象数组中删除元素? [英] In javascript, how do I remove an element from an array of objects?

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

问题描述

在javascript中,如何从对象数组中删除元素?
以下是代码:

In javascript, how do I remove an element from an array of objects? Here is the code:

$.fn.mapImage.deletePinpoint = function(image, pinpoint){
    var deleted = false;
    for (var i = 0; i < image.pinpoints.length; i++) {
        if(image.pinpoints[i].position == pinpoint.position){
            image.pinpoints.remove(i);
            deleted = true;
        }
        if(deleted){
            image.pinpoints[i].position -= 1;
        }
    }
    $('.edit-mode').find('div.dynamic-pinpoint-area').remove();
    $('.edit-mode').find('div.pinpoint-text').remove();
    $('.create-mode').find('div.static-pinpoint-area').remove();
    $('.create-mode').find('div.pinpoint-text').remove();

    $.fn.mapImage.load(image);

}

image.pinpoints 是对象数组。再次感谢大家!

image.pinpoints is the array of objects. Thanks again guys!

推荐答案

.splice 是w3schools给出的方法.com http://www.w3schools.com/jsref/jsref_splice.asp
要从索引x的数组中删除一个元素,你将拥有 trees.splice(x,x + 1); 这将删除x并在需要时返回它。

.splice is the method given at w3schools.com http://www.w3schools.com/jsref/jsref_splice.asp To remove one element from an array with index x you would have trees.splice(x,x+1); This removes x and returns it if you need it.

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

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