两个数组比较的问题

数组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"}]
慕娘9325324
浏览 530回答 2
2回答

慕的地10843

functionassign(a,b){for(leti=0,len=a.length;i

慕妹3242003

楼上代码其实有缺陷没有必要不相等就给a[i].name赋值为default应该是第二个for循环走完了如果a[i].name没有值,才进行赋值。functionassign(a,b){for(leti=0,len=a.length;i
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript