qq_875143287
2019-09-22 02:49
采纳率: 75%
浏览 703
已采纳

js中处理数组 如何在一个大的数组中提取一个小数组

已经有的数组

arry1[
0: "id"
1: "hosp_name"
2: "status"
3: "inner_status"
4: "create_user"
5: "manager_id"
6: "type"
7: "engineers"
8: "evaluate_userlist"
9: "saler_flag"
10: "department"
11: "mgrchg_flag"
12: "manager_idlist"
13: "overload_flag"]

arry2[
actual_deploy_time: "2017-01-01 00:00:00"
aio_qty: "21"
area: "上海市 上海市 宝山区"
area_list: (3) [1, 2, 9, __ob__: Observer]
card4g_qty: "21"
create_user: 710
create_username: "张珊"
depart_num: "21"
department: 1
deploy_engineers: "22,29,30"
engineers: "30,29"
evaluate_userlist: "30#29#710"
execute_userlist: [29, __ob__: Observer]
expect_dev_time: "2017-01-01 00:00:00"
expect_dev_time2: "2017-01-01 00:00:00"
first_flag: 0
fs_aio_type: 1
fs_server_type: 1
hosp_id: 10
hosp_name: "上海市宝山区顾村镇社区卫生服务中心"
id: 911
interface_engineers: "30"
interface_userlist: [30, __ob__: Observer]
manager_id: 10
manager_idlist: "10,27"
manager_name: "舒新"
memo: "21"
mgrchg_flag: 0
old_managerid: 10
overload_flag: 0
pad_qty: "21"
pc_qty: "21"
receiver_addr: "21"
receiver_name: "21"
receiver_tel: "21"
rel_manager: "李苏"
rel_manager_id: 710
route_qty: "21"
saler_flag: "0"
server_qty: "21"
status: 50
tracking_num: "21"
type: 1
view_interface_pre: 1
wechat_group: 1

]

需生成的数组

arry3[
0: id:911
1: hosp_name 上海市宝山区顾村镇社区卫生服务中心
2: status:1
3: inner_status:22
4: create_user:李苏
5: manager_id 12
6: type 1
7: engineers
8: "evaluate_userlist"
9: "saler_flag"
10: "department"
11: "mgrchg_flag"
12: "manager_idlist"
13: "overload_flag"]

等于根据第一个数组的对象名 取第二个数组的对象值

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • Json-Huang 2019-09-22 09:34
    已采纳
    if(arry1[0].id==arry2[1].id){
    arry[3].id=arry2[1].id;
    arry[3]. hosp_name =arry2[1]. hosp_name;
    
    //其他属性类似处理
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • 毕小宝 2019-09-22 14:14

    遍历第一个数组的属性,然后取第二个属性的值,生成一个 JSON 对象添加到第三个数组中,而且第二个不应该是数组而是对象。
    此外arry2 的内容有些字符串是错误的,有些列表中有未知变量,统一处理成合法的样子后的 demo

    function combine(){
        var arry1 = ["id","hosp_name","status","inner_status","create_user","manager_id","type","engineers","evaluate_userlist","saler_flag","department","mgrchg_flag","manager_idlist","overload_flag"];
    
        var arry2 = {
            "actual_deploy_time": "2017-01-01 00:00:00",
            "aio_qty": "21",
            "area": "上海市 上海市 宝山区",
            "area_list": "20",
            "card4g_qty": "21",
            "create_user": 710,
            "create_username": "张珊",
            "depart_num": "21",
            "department": 1,
            "deploy_engineers": "22,29,30",
            "engineers": "30,29",
            "evaluate_userlist": "30710",
            "execute_userlist": "20",
            "expect_dev_time": "2017-01-01 00:00:00",
            "expect_dev_time2": "2017-01-01 00:00:00",
            "first_flag": 0,
            "fs_aio_type": 1,
            "fs_server_type": 1,
            "hosp_id": 10,
            "hosp_name": "上海市宝山区顾村镇社区卫生服务中心",
            "id": 911,
            "interface_engineers": "30",
            "interface_userlist": "20",
            "manager_id": 10,
            "manager_idlist": "10,27",
            "manager_name": "舒新",
            "memo": "21",
            "mgrchg_flag": 0,
            "old_managerid": 10,
            "overload_flag": 0,
            "pad_qty": "21",
            "pc_qty": "21",
            "receiver_addr": "21",
            "receiver_name": "21",
            "receiver_tel": "21",
            "rel_manager": "李苏",
            "rel_manager_id": 710,
            "route_qty": "21",
            "saler_flag": "0",
            "server_qty": "21",
            "status": 50,
            "tracking_num": "21",
            "type": 1,
            "view_interface_pre": 1,
            "wechat_group": 1
        };
    
        var arry3 ={};
        //arry1[i] 是元素的 key arry2 是一个 Map ,arry3 是一个列表,每个元素是一个 Map
        for(var i=0;i<arry1.length;i++){
            //直接设置成 arry3 的属性。
           arry3[arry1[i]] = arry2[arry1[i]];
        }
    
        console.log(arry3);
    }
    

    合并的结果为:
    图片说明

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题