亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

30-seconds-code——Object

lbool / 3173人閱讀

摘要:英文文章來源于刪除對象中除指定鍵值的屬性用遞歸的方法用方法遍歷對象然后刪除不是在給定數(shù)組中的屬性如果你傳入,它將對該鍵所對應(yīng)的對象進行深度遍歷的變形非原著作對所有的鍵對應(yīng)的對象進行深度遍歷用方法遍歷對象然后刪除不是在給定數(shù)組中的屬性如

英文文章來源于:https://github.com/Chalarangelo/30-seconds-of-code/blob/master/README.md

Object cleanObj

刪除JSON對象中除指定鍵值的屬性.

用遞歸的方法
Object.keys() 方法遍歷JSON對象然后刪除不是include在給定數(shù)組中的屬性.
如果你傳入 childIndicator ,它將對該鍵所對應(yīng)的JSON對象進行深度遍歷.

const cleanObj = (obj, keysToKeep = [], childIndicator) => {
  Object.keys(obj).forEach(key => {
    if (key === childIndicator) {
      cleanObj(obj[key], keysToKeep, childIndicator);
    } else if (!keysToKeep.includes(key)) {
      delete obj[key];
    }
  })
}
/*
  const testObj = {a: 1, b: 2, children: {a: 1, b: 2}}
  cleanObj(testObj, ["a"],"children")
  console.log(testObj)// { a: 1, children : { a: 1}}
*/
cleanObj的變形(非原著作)

對所有的鍵對應(yīng)的JSON對象進行深度遍歷

Object.keys() 方法遍歷JSON對象然后刪除不是 include 在給定數(shù)組中的屬性.
如果給定的屬性值為object,則看傳入的 dep 是否為false,或者沒有第三個參數(shù),則直接刪除.
如果傳入 dep 為true,則進行深度遍歷.

const cleanObj = (obj, keysToKeep = [], dep=false) => {
  Object.keys(obj).forEach(key => {
    if(dep) {
      if (obj[key].constructor.name === "Object") {
        cleanObj(obj[key], keysToKeep, dep)
      } else {
        if (!keysToKeep.includes(key)) {
          delete obj[key];
        }
      }
    } else {
      if (!keysToKeep.includes(key)) {
        delete obj[key];
      }
    }
  })
  return obj
}
/*
  const testObj = {a:1, b:{a:1, b:2, c:3}, c:{a:1, b:2}, d: {a:1, b:2, c:3}}
  cleanObj(testObj, ["a"],"children")  // {a: 1, b: {a: 1}, c: {a: 1}, d: {a: 1, c: 3}}
*/
objectFromPairs

將一個鍵值對形式的數(shù)組轉(zhuǎn)化為對象
Array.reduce() 去創(chuàng)建對象并合并鍵值對.

const objectFromPairs = arr => arr.reduce((a, v) => (a[v[0]] = v[1], a), {});
// objectFromPairs([["a",1],["b",2]]) -> {a: 1, b: 2}
objectToPairs

將一個對象轉(zhuǎn)化為鍵值對形式的數(shù)組.

Object.keys() 將對象轉(zhuǎn)化為對象的鍵數(shù)組,然后 Array.map() 生成一個鍵值對的數(shù)組.

const objectToPairs = obj => Object.keys(obj).map(k => [k, obj[k]]);
// objectToPairs({a: 1, b: 2}) -> [["a",1],["b",2]])
orderBy

按對象屬性和排序規(guī)則排序?qū)ο髷?shù)組.

使用 Array.sort() 的自定義排序規(guī)則排序,根據(jù)傳入的 order排序方式,用解構(gòu)來實現(xiàn)指定的屬性的位置交換.
如果沒有傳入?yún)?shù) order 默認(rèn)值為asc.

const orderBy = (arr, props, orders) =>
  arr.sort((a, b) =>
    props.reduce((acc, prop, i) => {
      if (acc === 0) {
        const [p1, p2] = orders && orders[i] === "desc" ? [b[prop], a[prop]] : [a[prop], b[prop]];
        acc = p1 > p2 ? 1 : p1 < p2 ? -1 : 0;
      }
      return acc;
    }, 0)
  );
/*
const users = [{ "name": "fred",   "age": 48 },{ "name": "barney", "age": 36 },
  { "name": "fred",   "age": 40 },{ "name": "barney", "age": 34 }];
orderby(users, ["name", "age"], ["asc", "desc"]) -> [{name: "barney", age: 36}, {name: "barney", age: 34}, {name: "fred", age: 48}, {name: "fred", age: 40}]
orderby(users, ["name", "age"]) -> [{name: "barney", age: 34}, {name: "barney", age: 36}, {name: "fred", age: 40}, {name: "fred", age: 48}]
*/
select

從一個對象中檢索出選擇其所代表的屬性.

如果屬性不存在返回 undefined.

const select = (from, selector) =>
  selector.split(".").reduce((prev, cur) => prev && prev[cur], from);

// const obj = {selector: {to: {val: "val to select"}}};
// select(obj, "selector.to.val"); -> "val to select"
shallowClone

對象的淺拷貝.

Object.assign() 和一個 ({}) 來實現(xiàn)對源對象的淺拷貝.

const shallowClone = obj => Object.assign({}, obj);
/*
const a = { x: true, y: 1 };
const b = shallowClone(a);
a === b -> false
*/
truthCheckCollection

判斷第一個參數(shù)所代表的集合中的每一個對象中是否包含第二個參數(shù)所代表的屬性.

Array.every() 去檢查結(jié)合中每一個對象是否包含pre屬性.

const truthCheckCollection = (collection, pre) => (collection.every(obj => obj[pre]));
// truthCheckCollection([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}], "sex") -> true

更多關(guān)于30-seconds-code中文翻譯https://github.com/lvzhenbang/article/blob/master/js/30-seconds-code/index.md

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://www.ezyhdfw.cn/yun/90528.html

相關(guān)文章

  • 30-seconds-code——adapter

    摘要:英文文章來源于給定一個鍵值和一組參數(shù),但給定一個上下文時調(diào)用它們。 英文文章來源于:https://github.com/Chalarangelo/30-seconds-of-code/blob/master/README.md Adapter call 給定一個鍵值和一組參數(shù),但給定一個上下文時調(diào)用它們。 使用閉包調(diào)用存儲的鍵值與存儲的參數(shù) const call = ( key, ....

    dcr309duan 評論0 收藏0
  • 30-seconds-code——string

    摘要:英文文章來源于計算一個字符串中字符的所有排序情況使用遞歸遍歷字符串中的每個字符計算剩余字符串的所有順序用區(qū)合并該字符和剩余字符串的每種順序然后用將該字符串的所有順序合并到一個數(shù)組中當(dāng)字符串的等于或者時,是兩個基例字符串的首字母大寫用 英文文章來源于:https://github.com/Chalarangelo/30-seconds-of-code/blob/master/README...

    tinysun1234 評論0 收藏0
  • 30-seconds-code ——utility集合

    摘要:英文文章來源于返回參數(shù)列表中第一個非和的參數(shù)用實現(xiàn)返回第一個非參數(shù)返回一個用自定義函數(shù)中的函數(shù)是否返回來對中傳入的參數(shù)列表盡心過濾用去遍歷參數(shù)列表,用給定的函數(shù)的返回值來過濾參數(shù)列表返回給定值的基本類型返回給定值的構(gòu)造函數(shù)名字的小 Utility 英文文章來源于:https://github.com/Chalarangelo/30-seconds-of-code/blob/master...

    Jochen 評論0 收藏0
  • 30-seconds-code——browser

    摘要:顯示所有指定的元素用操作符和清除所有指定元素的屬性。使用了兩個事件監(jiān)聽器。將指定的數(shù)組元素轉(zhuǎn)換成元素標(biāo)簽,然后將它們插入指定的選擇器元素內(nèi)用和去生成一個元素標(biāo)簽列表復(fù)制一個字符串到剪切板。用去執(zhí)行復(fù)制到剪切板。 英文文章來源于:https://github.com/Chalarangelo/30-seconds-of-code/blob/master/README.md Browser...

    izhuhaodev 評論0 收藏0
  • 30-seconds-code——array

    摘要:英文文章來源于數(shù)組最大公約數(shù)計算數(shù)字?jǐn)?shù)組最大公約數(shù)用和運算式使用遞歸計算一個數(shù)字?jǐn)?shù)組的最大公約數(shù)數(shù)組最小公倍數(shù)求數(shù)字?jǐn)?shù)組的最小公倍數(shù)用和運算式使用遞歸計算一個數(shù)字?jǐn)?shù)組的最小公倍數(shù)返回一個數(shù)組中的最大值。 英文文章來源于:https://github.com/Chalarangelo/30-seconds-of-code/blob/master/README.md Array 數(shù)組最大公...

    adie 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<