从数组中删除 $$hashKey [英] remove $$hashKey from array

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

问题描述

$scope.appdata = [{name: '', position: '', email: ''}];

这是我在角度控制器中创建的数组.

This is the array which I created in angular controller.

然后我使用 push 方法将一些值插入到数组中:

Then I inserted some values in to array by using push method:

$scope.appdata.push({name: 'jenson raby', position: '2', email: 'jensonraby@gmail.com'});

这是插入后的数组值:

$$hashKey:"00F",name:"jenson raby",position:"2",email:"jensonraby@gmail.com",

这里在插入后向数组中添加了一个额外的 $$hashKey,但我需要将其从数组中删除.

Here an extra $$hashKey has been added to the array after insertion, but I need to remove this from array.

推荐答案

根据您需要将数组插入数据库的评论,我将假设您将其转换为 JSON 字符串,然后将其保存到数据库.如果那是不正确的,请告诉我,我会看看我是否可以修改此答案.

Based on your comment that you need to insert the array into the database, I'm going to assume that you are converting it a JSON string and then saving it to the DB. If that's incorrect, let me know and I'll see if I can revise this answer.

在将数组转换为 JSON 时,您有两种修改数组的选项.第一个是 angular.toJson,这是一个方便的方法在序列化数组(或对象)之前,自动去除任何带有前导 $$ 的属性名称.你会像这样使用它:

You have two options for modifying your array when converting it to JSON. The first is angular.toJson, which is a convenience method that automatically strips out any property names with a leading $$ prior to serializing the array (or object). You would use it like this:

var json = angular.toJson( $scope.appdata );

如果您需要更细粒度的控制,您应该使用的另一个选项是内置 JSON.stringify 函数.replacer 函数允许您在属性序列化为 JSON 之前对其进行过滤或更改.您可以像这样使用它来去除 $$hashKey:

The other option, which you should use if you need more fine grained control, is the replacer argument to the built-in JSON.stringify function. The replacer function allows you to filter or alter properties before they get serialized to JSON. You'd use it like this to strip $$hashKey:

var json = JSON.stringify( $scope.appdata, function( key, value ) {
    if( key === "$$hashKey" ) {
        return undefined;
    }

    return value;
});

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

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