ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

[Javascript] Build lodash.merge from Scratch

2022-04-11 01:32:27  阅读:257  来源: 互联网

标签:obj merge Scratch Javascript source mergeJSON return newValue


This lesson will demonstrate how to recreate a simplified version of the popular lodash.merge method from scratch.

First we'll create a test file with two objects that we want to merge together and demonstrate the output after running them through lodash.merge , then we'll go about creating our own version of the function.

Our initial version will support only one source object to merge in, and will support only the subset of JavaScript syntax supported by the JSON spec, including the following types:

  • booleannumberstring
  • object literal
  • array

After getting basic types to merge we'll show off two methods for merging arrays, either using concat or recursively going through the children and merging them.

Finally we'll build our own isObject function to check for object literals and recursively apply our mergeJSON function to any object literals in our source object.

And last we'll add support for multiple source objects to be merged in using a for...of loop.

Note

The mergeJSON method we create approximates the lodash.merge functionality but does not seek to replace it. It may be useful for some use cases, but primarily it's meant to be a tool for understanding how to merge objects with recursion.

function mergeJSON(obj, ...sources) {
  for (let source of sources) {
    for (let key in source) {
      if (source[key] == null) {
        continue;
      }
      obj[key] = replaceValue(obj[key], source[key]);
    }
  }

  return obj;
}

function replaceValue(value, newValue) {
  if (Array.isArray(value) && Array.isArray(newValue)) {
    return newValue.map((val, i) => {
      return replaceValue(value[i], val)
    });
  }
  if (isObject(value) && isObject(newValue)) {
    return mergeJSON(value, newValue);
  }

  return newValue;
}

function isObject(obj) {
  return obj && obj.constructor === Object;
}

module.exports = { mergeJSON };

index.js

import { mergeJSON } from "./merge";

const user1 = {
  name: "Joe",
  age: 33,
  links: {
    blog: "blog link",
    facebook: "facebook link"
  },
  interests: ["gaming"]
};
const user2 = {
  name: "Joe",
  age: 22,
  links: {
    twitter: "twitter link",
    blog: "another blog",
    facebook: null
  },
  interests: ["code"],
  extra: false
};
const user3 = {
  name: "Joe",
  age: 32,
  interests: ["gaming", "travling"]
};
const res = mergeJSON(user1, user2, user3);
console.log("mergeJSON", res);

 

标签:obj,merge,Scratch,Javascript,source,mergeJSON,return,newValue
来源: https://www.cnblogs.com/Answer1215/p/16128373.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有