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

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

使用 lodash 訪問對象

使用 lodash 訪問對象

神不在的星期二 2023-05-11 10:26:55
我正在嘗試使用 indexOf 在如下所示的數(shù)組中查找鍵const areaCode = [    {        "area_code": 656,        "city": "city1"    },    {        "area_code": 220,        "city": "city2"    },    {        "area_code": 221,        "city": "city3"    }]export default areaCode然后我試圖根據(jù) area_code 號(hào)碼獲取城市名稱const code = inputlet found = indexOf(areaCode, ["area_code", code]);const city = areaCode[found].city但是發(fā)現(xiàn)是-1,我做錯(cuò)了什么?
查看完整描述

3 回答

?
哆啦的時(shí)光機(jī)

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

你應(yīng)該使用 Lodash _.find 函數(shù)。


它會(huì)是這樣的:


const areaCode = [

{

    "area_code": 656,

    "city": "city1"

},

{

    "area_code": 220,

    "city": "city2"

},

{

    "area_code": 221,

    "city": "city3"

}]

const code = input;

const found = _.find(areaCode, function(a){ return a.area_code == code });

console.log(found.city)

const found 將保存匹配區(qū)域。


https://lodash.com/docs/4.17.15#find


查看完整回答
反對 回復(fù) 2023-05-11
?
慕的地10843

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

我相信_.findIndex()

let?found?=?findIndex(areaCode,?["area_code",?code]);


查看完整回答
反對 回復(fù) 2023-05-11
?
吃雞游戲

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

根據(jù)文檔_.indexOf將執(zhí)行SameValueZero比較來定位索引。簡而言之,因?yàn)?code>indexOf(data, item)它會(huì)嘗試使用===to compareitemdata.

相反,您可以使用which accepts將被接受的_.findIndex常用簡寫:_.matchesProperty_.iteratee

const { findIndex } = _;


const areaCode = [

? ? {

? ? ? ? "area_code": 656,

? ? ? ? "city": "city1"

? ? },

? ? {

? ? ? ? "area_code": 220,

? ? ? ? "city": "city2"

? ? },

? ? {

? ? ? ? "area_code": 221,

? ? ? ? "city": "city3"

? ? }]


const code = 220;


let found = findIndex(areaCode, ["area_code", code]);

console.log("index:", found);


const city = areaCode[found].city

console.log("city:", city);

<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.20/lodash.min.js"></script>


雖然,鑒于您的用法,您可能想要_.find


const { find } = _;


const areaCode = [

? ? {

? ? ? ? "area_code": 656,

? ? ? ? "city": "city1"

? ? },

? ? {

? ? ? ? "area_code": 220,

? ? ? ? "city": "city2"

? ? },

? ? {

? ? ? ? "area_code": 221,

? ? ? ? "city": "city3"

? ? }]


const code = 220;


let found = find(areaCode, ["area_code", code]);

console.log("index:", found);


const city = found.city

console.log("city:", city);

<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.20/lodash.min.js"></script>



查看完整回答
反對 回復(fù) 2023-05-11
  • 3 回答
  • 0 關(guān)注
  • 173 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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