js怎么将下面的函数简化

function mergeJsonObject(jsonObj1, jsonObj2, jsonObj3, jsonObj4, jsonObj5) {

    let resultJsonObject = {};


        function jsonObj(jsonObj) {

              for (let attr in jsonObj) {

                   resultJsonObject[attr] = jsonObj[attr];

                   }

              }


        jsonObj(jsonObj1);

        jsonObj(jsonObj2);

        jsonObj(jsonObj3);

        jsonObj(jsonObj4);

        jsonObj(jsonObj5);

        return resultJsonObject;

  }

  

可以用arguments吗,传入参数个数不固定


回首忆惘然
浏览 678回答 8
8回答

慕田峪4524236

function mergeJsonObject(...args) {    let resultJsonObject = {};        function jsonObj(jsonObj) {              for (let attr in jsonObj) {                   resultJsonObject[attr] = jsonObj[attr];                   }              }        args.forEach(jsonObj)        return resultJsonObject;  }

精慕HU

function mergeJsonObject(...jsonObjs) {    let resultJsonObject = {};                jsonObjs.forEach(function(jsonObj) {              for (let attr in jsonObj) {                   resultJsonObject[attr] = jsonObj[attr];              }        })        return resultJsonObject;  }更简化一点的,直接Object.assign({}, jsonObj1, jsonObj2)

临摹微笑

看以什么规范,如果 ES6 的话可以:function merge(...jsons) {  return jsons.reduce((memo, json) => {    return Object.assign(memo, json);  }, {});}

慕容森

function mergeJsonObject(...args) {    let resultJsonObject = {}    args.map(function(val){        Object.assign(resultJsonObject, val)            })    return resultJsonObject}

PIPIONE

function mergeJsonObject(jsonObj1, jsonObj2, jsonObj3, jsonObj4, jsonObj5) {  let resultJsonObject = {};   let allJson=arguments;    for(let index in allJson){      for(let attr in allJson[index]){        resultJsonObject[attr] = allJson[index][attr];        }    }    return resultJsonObject;  }

慕仙森

例子参考一下,看看是不是你想要的var aa = function(){for (var i=0; i<arguments.length; i++){&nbsp; &nbsp; if (typeof (arguments[i]) == "object"){&nbsp; &nbsp; &nbsp; &nbsp; console.log(arguments[i])&nbsp; &nbsp; }}}

HUWWW

同属性覆盖与否的问题没有解决const mereJsons = () {&nbsp; &nbsp; const args = [...arguments];&nbsp; &nbsp; return args.reduce((prev, current) => {&nbsp; &nbsp; &nbsp; for(let key in current){&nbsp; &nbsp; &nbsp; &nbsp; prev[key] = current[key];&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; return prev;&nbsp; &nbsp; });}

慕码人8056858

function merge() {&nbsp; &nbsp; var args = [];&nbsp; &nbsp; for (var _i = 0; _i < arguments.length; _i++) {&nbsp; &nbsp; &nbsp; &nbsp; args[_i] = arguments[_i];&nbsp; &nbsp; }&nbsp; &nbsp; var merged = {};&nbsp; &nbsp; function generateObject(target, object) {&nbsp; &nbsp; &nbsp; &nbsp; if (target === void 0) { target = {}; }&nbsp; &nbsp; &nbsp; &nbsp; for (var key in object) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (object.hasOwnProperty(key)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; target[key] = object[key];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return target;&nbsp; &nbsp; }&nbsp; &nbsp; for (var i = 0; i < args.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; var arg = args[i];&nbsp; &nbsp; &nbsp; &nbsp; if (arg) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (isArray(arg)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (var i_1 = 0; i_1 < arg.length; i_1++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var argItem = arg[i_1];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (isPlainObject(argItem)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; merged = generateObject(merged, argItem);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (!isDate(argItem)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; merged[argItem] = argItem;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (isPlainObject(arg)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; merged = generateObject(merged, arg);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (isString(arg) || isNumber(arg)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; merged[arg] = arg;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return merged;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript