两个数组比较的问题,并从为数组a添加数据b的属性

cyyjm 2018-05-14 10:50:04
数组a:
[
{"user_num":"75925","MeterNo":"50005445","BookNo":"51010"},
{"user_num":"75921","MeterNo":"50005445","BookNo":"51010"},
{"user_num":"7595","MeterNo":"50005445","BookNo":"51010"},
{"user_num":"75984","MeterNo":"50005445","BookNo":"51010"},
{"user_num":"759020","MeterNo":"50005445","BookNo":"51010"},
{"user_num":"75930","MeterNo":"50005445","BookNo":"51010"},
{"user_num":"75911","MeterNo":"50005445","BookNo":"51010"},
{"user_num":"75931","MeterNo":"50005445","BookNo":"51010"},
{"user_num":"75912","MeterNo":"50005445","BookNo":"51010"},
{"user_num":"75932","MeterNo":"50005445","BookNo":"51010"},
{"user_num":"75913","MeterNo":"50005445","BookNo":"51010"}
]

数组b:
[

{user_num:75925,name:"范小明"},
{user_num:75921,name:"李大霄"},
{user_num:7595,name:"刘士雨"},
{user_num:75984,name:"廖冰"},
{user_num:7598020,name:"李刚"},
{user_num:7597,name:"李世民"},
{user_num:75930,name:"刘彬彬"},
{user_num:75911,name:"森美"}
]

我想得到这样的效果,遍历数组a的user_num,如果,其值在数组b里,则取出该数组b的name属性,并添加到数组a中。如果user_num没有在数组b中,在添加一个默认值name属性(可以设为default)给数组a.

最后,数组a将变成这样:
[
{"user_num":"75925","MeterNo":"50005445","BookNo":"51010","name":"范小明"},
{"user_num":"75921","MeterNo":"50005445","BookNo":"51010","name":"李大霄"},
{"user_num":"7595","MeterNo":"50005445","BookNo":"51010","name":"刘士雨"},
{"user_num":"75984","MeterNo":"50005445","BookNo":"51010","name":"廖冰"},
{"user_num":"759020","MeterNo":"50005445","BookNo":"51010","name":"李刚"},
{"user_num":"75930","MeterNo":"50005445","BookNo":"51010","name":"刘彬彬"},
{"user_num":"75911","MeterNo":"50005445","BookNo":"51010","name":"森美"},
{"user_num":"75931","MeterNo":"50005445","BookNo":"51010","name":"default"},
{"user_num":"75912","MeterNo":"50005445","BookNo":"51010","name":"default"},
{"user_num":"75932","MeterNo":"50005445","BookNo":"51010","name":"default"},
{"user_num":"75913","MeterNo":"50005445","BookNo":"51010","name":"default"}
]
...全文
172 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
functionsub 2018-05-14
  • 打赏
  • 举报
回复
var a = [
{"user_num":"75925","MeterNo":"50005445","BookNo":"51010"},
{"user_num":"75921","MeterNo":"50005445","BookNo":"51010"},
{"user_num":"7595","MeterNo":"50005445","BookNo":"51010"},
{"user_num":"75984","MeterNo":"50005445","BookNo":"51010"},
{"user_num":"759020","MeterNo":"50005445","BookNo":"51010"},
{"user_num":"75930","MeterNo":"50005445","BookNo":"51010"},
{"user_num":"75911","MeterNo":"50005445","BookNo":"51010"},
{"user_num":"75931","MeterNo":"50005445","BookNo":"51010"},
{"user_num":"75912","MeterNo":"50005445","BookNo":"51010"},
{"user_num":"75932","MeterNo":"50005445","BookNo":"51010"},
{"user_num":"75913","MeterNo":"50005445","BookNo":"51010"}
]
var b = [

                            {user_num:75925,name:"范小明"},
                            {user_num:75921,name:"李大霄"},
                            {user_num:7595,name:"刘士雨"},
                            {user_num:75984,name:"廖冰"},
                            {user_num:7598020,name:"李刚"},
                            {user_num:7597,name:"李世民"},
                            {user_num:75930,name:"刘彬彬"},
                            {user_num:75911,name:"森美"}
];
for(var i=0;i<a.length;i++){
    var isIn = false;
    for(var j=0;j<b.length;j++){
        if(a[i].user_num == b[j].user_num){
            isIn = true;
            a[i].name = b[j].name;
            break;
        }
    }
    if(!isIn){
        a[i].name = 'default';
    }
}
console.log(a)

87,972

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧