Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2020-03-08 对象合并 #3

Open
fanmingfei opened this issue Mar 8, 2020 · 10 comments
Open

2020-03-08 对象合并 #3

fanmingfei opened this issue Mar 8, 2020 · 10 comments

Comments

@fanmingfei
Copy link
Member

image
image

不需要考虑浏览器兼容性。

@markmcsong
Copy link

function merge(json1, json2) {
  if(!json1 || !json2) return json1 || json2
  const result = {}
  for (let prop in json1) {
    result[prop] = json1[prop]
  }
  for (let prop in json2) {
    // 同名属性值为 Object
    if (Reflect.has(json1, prop) && isObject(json2[prop]) && isObject(json1[prop])) {
      result[prop] = merge(json1[prop], json2[prop])
    } else {
      result[prop] = json2[prop]
    }
  }
  return result
}

const isObject = el => {
  return Object.prototype.toString.call(el) === '[object Object]'
}

@SunYiwen
Copy link

SunYiwen commented Mar 8, 2020

function merge(json1,json2){
    const json  = {};
    let propName = Object.keys(json2);
    for(let prop in json1 ){
        json[prop] = json1[prop];
    }
    propName.map(function (item) {
        if(json[item] === undefined){
            json[item] = json2[item];
        }
        else{
            if(typeof json[item] === "object"&& (!Array.isArray(json[item]))){
                json[item] = merge(json[item],json2[item]);
            }
            else{
                json[item] = json2[item];
            }
        }
    })
    return json;
}

@fanmingfei fanmingfei changed the title 2020-03-08 数组合并 2020-03-08 对象合并 Mar 8, 2020
@fanmingfei
Copy link
Member Author

fanmingfei commented Mar 8, 2020

function merge(json1, json2) {
  const json = JSON.parse(JSON.stringify(json1))
  const realMerge = (a, b) => {
    for(let key in b) {
      if (Object.prototype.toString.call(a[key]) !== '[object Object]') {
        a[key] = b[key]
      } else {
        realMerge(a[key], b[key])
      }
    }
  }
  realMerge(json, json2)
  return json
 }

@LiZhaji
Copy link

LiZhaji commented Mar 8, 2020

 function merge(json1, json2) {
   const json = {...json1}
   for (const key in json2) {
      Object.assign(json, 
      {[key]: Object.prototype.toString.call(json2[key] ) === '[object Object]' 
      ? merge(json1[key], json2[key]) : json2[key]})
   }
   return json
 }
merge(json1, json2)

@xiaosen7
Copy link

xiaosen7 commented Mar 8, 2020

function merge(...objs) {
return objs.reduceRight((right, left) => {
for (const key in right) {
if ((Object.prototype.toString.call(right[key]) === '[object Object]') && (Object.prototype.toString.call(left[key]) === '[object Object]')) {
merge(left[key], right[key]);
} else {
left[key] = right[key];
}
}
return left;
});
}

@terrykingcha
Copy link
Member

function getType(value) {
  try {
    return Object.prototype.toString.call(value).match(/\b(\w+)\]$/)[1];
  } catch(e) {
    return '';
  }
}

function merge(source, target) {
  const result = {};
  const keys = Object.keys(source);
  
  Object.keys(target).forEach(key => !keys.includes(key) && keys.push(key));
  

  for (const key of keys) {
    const sourceType = getType(source[key]);
    const targetType = getType(target[key]);
    if (sourceType === targetType) {
      if (sourceType === 'Object') {
        result[key] = merge(source[key], target[key]);
      } else {
        result[key] = target[key];
      }
    } else if (targetType === 'Undefined') {
       result[key] = source[key];
    } else if (sourceType === 'Undefined') {
       result[key] = target[key];
    } else {
       throw new Error('wrong data sturcture');
    }
  }

  return result;
}

@KobeBeyond
Copy link

function merge(target, source) {
return Object.assign(target, source)
}
// 详见https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/assign

@fanmingfei
Copy link
Member Author

function merge(target, source) {
return Object.assign(target, source)
}
// 详见https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/assign

你这个不太行吧,对象里面但对象是要合并但。

@fanmingfei
Copy link
Member Author

fanmingfei commented Mar 8, 2020

function getType(value) {
  try {
    return Object.prototype.toString.call(value).match(/\b(\w+)\]$/)[1];
  } catch(e) {
    return '';
  }
}

function merge(source, target) {
  const result = {};
  const keys = Object.keys(source);
  
  Object.keys(target).forEach(key => !keys.includes(key) && keys.push(key));
  

  for (const key of keys) {
    const sourceType = getType(source[key]);
    const targetType = getType(target[key]);
    if (sourceType === targetType) {
      if (sourceType === 'Object') {
        result[key] = merge(source[key], target[key]);
      } else {
        result[key] = target[key];
      }
    } else if (targetType === 'Undefined') {
       result[key] = source[key];
    } else if (sourceType === 'Undefined') {
       result[key] = target[key];
    } else {
       throw new Error('wrong data sturcture');
    }
  }

  return result;
}

const json1 = {
a: {a:1}
}
const json2 = {
a: 1
}

@terrykingcha 不能用数字覆盖对象。

@KobeBeyond
Copy link

function merge(target, source) {
return Object.assign(target, source)
}
// 详见https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/assign

你这个不太行吧,对象里面但对象是要合并但。

嗯你说得对!我没看清题🤪

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants