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

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

在數(shù)組中查找對(duì)象,查找 id 匹配子字符串的對(duì)象

在數(shù)組中查找對(duì)象,查找 id 匹配子字符串的對(duì)象

烙印99 2022-12-29 16:14:54
我從 API 獲得了很多數(shù)組,但 id 的計(jì)數(shù)不同,我需要檢查匹配[id = region...]并只獲得這個(gè)對(duì)象。我如何使用正則表達(dá)式和來(lái)做到這一點(diǎn)find?(5) [{…}, {…}, {…}, {…}, {…}]0: {id: "address.7576869587107444", type: "Feature", place_type: Array(1), relevance: 1, properties: {…}, …}1: {id: "postcode.12959148828066430", type: "Feature", place_type: Array(1), relevance: 1, properties: {…}, …}2: {id: "place.14392640799224870", type: "Feature", place_type: Array(1), relevance: 1, properties: {…}, …}3: {id: "region.9375820343691660", type: "Feature", place_type: Array(1), relevance: 1, properties: {…}, …}4: {id: "country.13200156005766020", type: "Feature", place_type: Array(1), relevance: 1, properties: {…}, …}length: 5__proto__: Array(0).then(res => {        let region = /region/gi        console.log(res.data.features.find(place => place.id == region.test(place)))        setAdrress(res.data)      })
查看完整描述

2 回答

?
江戶川亂折騰

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

使用String#match

console.log(res.data.features.find(place => place.id.match(region)))

const arr = [{id: "address.7576869587107444", type: "Feature"},

{id: "postcode.12959148828066430", type: "Feature"},

{id: "place.14392640799224870", type: "Feature"},

{id: "region.9375820343691660", type: "Feature"},

{id: "country.13200156005766020", type: "Feature"}];

let region = /region/gi;

console.log(arr.find(place => place.id.match(region)))


查看完整回答
反對(duì) 回復(fù) 2022-12-29
?
慕哥9229398

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

您可以過(guò)濾數(shù)組并使用正則表達(dá)式檢查以實(shí)現(xiàn)該目的。

var regex = RegExp(/^region/);
var filtered_arr = arr.filter(obj => regex.test(obj.id));

filtered_arr將包含您的 id 開頭的對(duì)象數(shù)組region

使用正則表達(dá)式將確保過(guò)濾所有以開頭的字符串。region使用 match 之類的東西會(huì)找到region字符串中任何位置的 id。由于我沒(méi)有足夠的聲譽(yù),我無(wú)法對(duì)第一篇文章發(fā)表評(píng)論。:)


查看完整回答
反對(duì) 回復(fù) 2022-12-29
  • 2 回答
  • 0 關(guān)注
  • 166 瀏覽
慕課專欄
更多

添加回答

舉報(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)