标签云

微信群

扫码加入我们

WeChat QR Code

从数组中删除对象的键值,JavaScript [复制]

I have an array that looks like this:

$scope.quotes =
    [
        {
            'quote': 'foo',
            'author': 'bar',
            'source': 'foobar',
            'first': 'slideout',
        },
        {
            ...
        },
    ]

I'm trying to remove the key-value first: slideout from the array. like this:

delete $scope.quotes[0][first];

I also tried:

delete $scope.quotes[0].first;


Your second attempt will work. If it doesn't then there's some other issue.

2018年05月27日34分06秒

Kami: That's different. He's trying to remove a key/value pair from a plain object that happens to be in an Array.

2018年05月27日34分06秒

And the first one would work if you had quotes around first. E.g., both delete $scope.quotes[0]['first']; and delete $scope.quotes[0].first; are correct.

2018年05月27日34分06秒

$scope = {}; $scope.quotes = [ { 'quote': 'foo', 'author': 'bar', 'source': 'foobar', 'first': 'slideout', } ]; delete $scope.quotes[0]["first"]; $scope.quotes[0]; // What you have should work.

2018年05月27日34分06秒