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

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

    • json对象添加属性
    • json数组添加元素
    • 对象内部数组中追加元素

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条数据
js向object对象添加属性和向数组中追加元素_第1张图片将follow_list_new对象 08/27 数组中的元素循环追加到follow_list_old对象中 08/27的数组内
js向object对象添加属性和向数组中追加元素_第2张图片

你可能感兴趣的:(Javascript,Jquery,Html,javascript,jquery,html)