js向object对象添加属性和向数组中追加元素

231 篇文章 5 订阅
188 篇文章 1 订阅

js向object对象添加属性和向数组中追加元素

json对象添加属性

var jsonObj={
      'param1':11,
      'param2' :22
};
jsonObj. param3='33';

json数组添加元素

 
var jsonstr="[{'name':'a','value':1},{'name':'b','value':2}]";
var jsonarray = eval('('+jsonstr+')');
var arr  =
     {
         "name" : $('#names').val(),
         "value" : $('#values').val()
     }
jsonarray.push(arr);
var jsonObj={};// 定义一个json对象
jsonObj.array1=["2","4"];// 增加一个新属性,此属性是数组jsonObj.array1[jsonObj.array1.length]='6';// 数组arr1追加一个元素
jsonObj.array2=["1","3","5"];//json对象中添加元素console.log(jsonObj);

对象内部数组中追加元素

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
    <meta name="format-detection" content="telephone=no"/>
    <title>对象合并</title>
</head>
<body>
<script>

    var follow_list_old = {
        "09/06": [
            {
                "follow_id": "298",
                "follow_time": "14:20",
                "follow_content": "已沟通,老客户,未接通电话,再次购买,无意向,vip客户,抽奖客户,新客户",
                "operator": "Gjx"
            },
            {
                "follow_id": "297",
                "follow_time": "14:18",
                "follow_content": "未接通电话",
                "operator": "Gjx"
            },
            {
                "follow_id": "296",
                "follow_time": "14:18",
                "follow_content": "老客户,抽奖客户",
                "operator": "Gjx"
            }
        ],
        "08/28": [
            {
                "follow_id": "111",
                "follow_time": "13:45",
                "follow_content": "无意向,抽奖客户,已沟通",
                "operator": "Gjx"
            }
        ],
        "08/27": [
            {
                "follow_id": "107",
                "follow_time": "17:44",
                "follow_content": "抽奖客户,新客户",
                "operator": "Gjx"
            },
            {
                "follow_id": "104",
                "follow_time": "15:38",
                "follow_content": "再次购买,已沟通",
                "operator": "Gjx"
            },
            {
                "follow_id": "103",
                "follow_time": "15:38",
                "follow_content": "抽奖客户,已沟通",
                "operator": "Gjx"
            },
            {
                "follow_id": "102",
                "follow_time": "14:56",
                "follow_content": "再次购买,已沟通",
                "operator": "Gjx"
            },
            {
                "follow_id": "101",
                "follow_time": "14:56",
                "follow_content": "已沟通",
                "operator": "Gjx"
            },
            {
                "follow_id": "100",
                "follow_time": "14:55",
                "follow_content": "已沟通",
                "operator": "Gjx"
            }
        ]
    };

    console.log('follow_list_old==', follow_list_old);


    var follow_list_new = {
        "08/27": [
            {
                "follow_id": "99",
                "follow_time": "14:55",
                "follow_content": "ddss",
                "operator": "Gjx"
            },
            {
                "follow_id": "98",
                "follow_time": "14:55",
                "follow_content": "再次购买,已沟通,老客户",
                "operator": "Gjx"
            },
            {
                "follow_id": "97",
                "follow_time": "14:55",
                "follow_content": "再次购买,已沟通",
                "operator": "Gjx"
            },
            {
                "follow_id": "96",
                "follow_time": "14:53",
                "follow_content": "再次购买,已沟通,已沟通,再次购买",
                "operator": "Gjx"
            },
            {
                "follow_id": "95",
                "follow_time": "14:52",
                "follow_content": "已沟通,抽奖客户",
                "operator": "Gjx"
            },
            {
                "follow_id": "94",
                "follow_time": "14:52",
                "follow_content": "已沟通,已沟通",
                "operator": "Gjx"
            },
            {
                "follow_id": "93",
                "follow_time": "14:51",
                "follow_content": "再次购买",
                "operator": "Gjx"
            },
            {
                "follow_id": "92",
                "follow_time": "14:48",
                "follow_content": "再次购买,已沟通,未接通电话",
                "operator": "Gjx"
            },
            {
                "follow_id": "91",
                "follow_time": "14:48",
                "follow_content": "再次购买,已沟通,抽奖客户,再次购买,未接通电话",
                "operator": "Gjx"
            },
            {
                "follow_id": "90",
                "follow_time": "14:48",
                "follow_content": "再次购买,已沟通,老客户,未接通电话",
                "operator": "Gjx"
            }
        ]
    };

    console.log('follow_list_new==', follow_list_new);

    for (var key in follow_list_new) {
        if (follow_list_new.hasOwnProperty(key) === true) {
            //此处hasOwnProperty是判断自有属性,使用 for in 循环遍历对象的属性时,原型链上的所有属性都将被访问会避免原型对象扩展带来的干扰
            for (var keya in follow_list_new[key]) {
                follow_list_old[key][follow_list_old[key].length] = follow_list_new[key][keya];
            }
        }
    }

    console.log('follow_list_old===', follow_list_old);

</script>
</body>
</html>

可以看到follow_list_new有10条数据
在这里插入图片描述将follow_list_new对象 08/27 数组中的元素循环追加到follow_list_old对象中 08/27的数组内
在这里插入图片描述

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值