在javascript中按键过滤

我想在javascript中按键过滤。


我有一个 ajax 函数,在传递 id 时,将导致obj1一个接一个的成功。我想通过合并所有结果key并推送到单个对象。如何在 javascript 中执行以下操作。


var id =["trans", "fund"]

setTimeout(()=>{

      id.map((e)=>this.static(e))

    }, 2000)


static(id){

var li = $.ajax({

           url: "/en",

          method: 'get',

          global: false,

          async: false,

          data: {

            providers: id

          },

          success: function (data) {

            return data;

          }

        }).responseText;

  var obj1=data; //I need to combine and store data in expected output format

}



//on first call

var obj1={

  country: "SG",

  trans: [{

    rate: 20,

    members: 100

  }]

}

//on second call

var obj1={

  country: "TH",

  fund: [{

    rate: 20,

    members: 100

  }]

}


预期输出:


var result ={

  trans: [{

    rate: 20,

    members: 100

  }],

  fund: [{

    rate: 20,

    members: 100

  }]

}


紫衣仙女
浏览 148回答 3
3回答

PIPIONE

尝试做这样的事情。我假设你的钥匙是固定的。  var output = {};  var id = ["trans", "fund"]  //on first call  var obj1 = {    country: "SG",    trans: [{      rate: 20,      members: 100    }]  }  //on second call  var obj2 = {    country: "TH",    fund: [{      rate: 20,      members: 100    }]  }  function getObj(selectedId) {    console.log(id); // global variable.    if(!id) return;    id.forEach(x => {      let obj1Key = obj1[x];      if (obj1Key) output[x] = obj1Key;      let obj2Key = obj2[x];      if (obj2Key) output[x] = obj2Key;    })   console.log('output object', output);   console.log('JSON:', JSON.stringify(output));  }

蝴蝶刀刀

您可以尝试如下,如果结果已经有值,这将合并值// var id =["trans", "fund"]; var result = {}; function getObj(obj){   id.map(val => {      if(result[val]) { result[val] = [...result[val], ...obj[val]]}      else result[val] = obj[val]   }) }//on first call    var obj1={      country: "SG",      trans: [{        rate: 20,        members: 100      }]    }    getObj(obj1)/*{  trans: [{    rate: 20,    members: 100  }]}*/    //on second call    var obj1={      country: "TH",      fund: [{        rate: 20,        members: 100      }]    }getObj(obj2)/*{  trans: [{    rate: 20,    members: 100   }],   fund: [{     rate: 20,     members: 100   }]}*/

函数式编程

试试这个:var id =["trans", "fund"];var result = {};//on first callvar obj1={&nbsp; &nbsp; country: "SG",&nbsp; &nbsp; trans: [{&nbsp; &nbsp; &nbsp; &nbsp; rate: 20,&nbsp; &nbsp; &nbsp; &nbsp; members: 100&nbsp; &nbsp; }]}result = getObj(result,obj1);//on second callvar obj1={&nbsp; &nbsp; country: "TH",&nbsp; &nbsp; fund: [{&nbsp; &nbsp; &nbsp; &nbsp; rate: 20,&nbsp; &nbsp; &nbsp; &nbsp; members: 100&nbsp; &nbsp; }]}result = getObj(result,obj1);console.log(result);function getObj(result,obj){&nbsp; &nbsp; for (let i = 0; i < id.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; if (obj[id[i]]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result[id[i]] =&nbsp; obj[id[i]];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return result;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return result;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript