第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

將 JSON 對(duì)象鍵轉(zhuǎn)換為小寫

將 JSON 對(duì)象鍵轉(zhuǎn)換為小寫

ibeautiful 2023-08-10 15:48:51
所以我有以下 JSON 對(duì)象:var myObj = {   Name: "Paul",   Address: "27 Light Avenue"}我想將其鍵轉(zhuǎn)換為小寫,這樣我會(huì)得到:var newObj = {   name: "Paul",   address: "27 Light Avenue"}我嘗試了以下方法:var newObj = mapLower(myObj, function(field) {    return field.toLowerCase();})function mapLower(obj, mapFunc) {    return Object.keys(obj).reduce(function(result,key) {         result[key] = mapFunc(obj[key])         return result;    }, {})}但我收到一條錯(cuò)誤消息“Uncaught TypeError: field.toLowerCase is not a function”。
查看完整描述

4 回答

?
幕布斯6054654

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超7個(gè)贊

我真的不確定你想用你的函數(shù)做什么,mapLower但你似乎只傳遞一個(gè)參數(shù),即對(duì)象值。


嘗試這樣的事情(不是遞歸)


var myObj = {

   Name: "Paul",

   Address: "27 Light Avenue"

}


const t1 = performance.now()


const newObj = Object.fromEntries(Object.entries(myObj).map(([ key, val ]) =>

  [ key.toLowerCase(), val ]))


const t2 = performance.now()


console.info(newObj)

console.log(`Operation took ${t2 - t1}ms`)

這將獲取所有對(duì)象條目(鍵/值對(duì)的數(shù)組),并將它們映射到鍵小寫的新數(shù)組,然后從這些映射條目創(chuàng)建新對(duì)象。


如果您需要它來(lái)處理嵌套對(duì)象,您將需要使用遞歸版本


var myObj = {

  Name: "Paul",

  Address: {

    Street: "27 Light Avenue"

  }

}


// Helper function for detection objects

const isObject = obj => 

  Object.prototype.toString.call(obj) === "[object Object]"


// The entry point for recursion, iterates and maps object properties

const lowerCaseObjectKeys = obj =>

  Object.fromEntries(Object.entries(obj).map(objectKeyMapper))

  

// Converts keys to lowercase, detects object values

// and sends them off for further conversion

const objectKeyMapper = ([ key, val ]) =>

  ([

    key.toLowerCase(), 

    isObject(val)

      ? lowerCaseObjectKeys(val)

      : val

  ])


const t1 = performance.now()


const newObj = lowerCaseObjectKeys(myObj)


const t2 = performance.now()


console.info(newObj)

console.log(`Operation took ${t2 - t1}ms`)


查看完整回答
反對(duì) 回復(fù) 2023-08-10
?
慕田峪7331174

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超13個(gè)贊

這將解決您的問(wèn)題:


var myObj = {

   Name: "Paul",

   Address: "27 Light Avenue"

}


let result = Object.keys(myObj).reduce((prev, current) => 

({ ...prev, [current.toLowerCase()]: myObj[current]}), {})


console.log(result)


查看完整回答
反對(duì) 回復(fù) 2023-08-10
?
搖曳的薔薇

TA貢獻(xiàn)1793條經(jīng)驗(yàn) 獲得超6個(gè)贊

var myObj = {

   Name: "Paul",

   Address: "27 Light Avenue"

}


Object.keys(myObj).map(key => {

  if(key.toLowerCase() != key){

    myObj[key.toLowerCase()] = myObj[key];

    delete myObj[key];

  }

});


console.log(myObj);


查看完整回答
反對(duì) 回復(fù) 2023-08-10
?
HUX布斯

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超6個(gè)贊

使用json-case-convertor


const jcc = require('json-case-convertor')

var myObj = {

   Name: "Paul",

   Address: "27 Light Avenue"

}

const lowerCase = jcc.lowerCaseKeys(myObj)

包鏈接: https: //www.npmjs.com/package/json-case-convertor


查看完整回答
反對(duì) 回復(fù) 2023-08-10
  • 4 回答
  • 0 關(guān)注
  • 266 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)