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

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

在對(duì)象中查找特定名稱中的值

在對(duì)象中查找特定名稱中的值

侃侃爾雅 2022-05-14 14:59:23
如何從對(duì)象中獲取特定值,這是我的示例對(duì)象和偽代碼來獲得我的預(yù)期結(jié)果:items = [    {code: "1", fruits: "APPLE", color: "red"},    {code: "2", fruits: "BANANA", color: "dummy"},    {code: "3", fruits: "BANANA", color: "anotherDummy"},    {code: "4", fruits: "ORANGE", color: "orange"}]items.find("BANANA")我應(yīng)該循環(huán)它還是有一個(gè)可以輕松找到值的函數(shù)。預(yù)期結(jié)果:2,"BANANA","yellow"3,"BANANA","anotherDummy"
查看完整描述

3 回答

?
呼如林

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

你可以你filter的數(shù)組


const items = [

  {code: "1", fruits: "APPLE", color: "red"},

  {code: "2", fruits: "BANANA", color: "dummy"},

  {code: "3", fruits: "BANANA", color: "anotherDummy"},

  {code: "4", fruits: "ORANGE", color: "orange"}

];


const keyword = 'BANANA';


const filter = items.filter(item => (item.code === keyword || item.fruits === keyword ||item.color === keyword));


console.log(filter);

也適用于多個(gè)過濾器/搜索


const items = [

  {code: "1", fruits: "APPLE", color: "red"},

  {code: "2", fruits: "BANANA", color: "dummy"},

  {code: "3", fruits: "BANANA", color: "anotherDummy"},

  {code: "4", fruits: "ORANGE", color: "orange"}

];


const keyword = ['APPLE', 'BANANA'];


const filter = items.filter(item => (

  keyword.includes(item.code)

  || keyword.includes(item.fruits)

  || keyword.includes(item.color)

));


console.log(filter);


查看完整回答
反對(duì) 回復(fù) 2022-05-14
?
LEATH

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

希望這可以幫助 -

  • 此代碼將返回包含找到的項(xiàng)目的數(shù)組

  • 這將比較每個(gè)項(xiàng)目的所有屬性/鍵給出的值。

  • 此外,最好創(chuàng)建自定義(創(chuàng)建 findCustom() 函數(shù))而不是覆蓋內(nèi)置的 find() 方法。

   var items = [{code: "1", fruits: "APPLE", color: "red"},

    {code: "2", fruits: "BANANA", color: "dummy"},

    {code: "3", fruits: "BANANA", color: "anotherDummy"},

    {code: "4", fruits: "ORANGE", color: "orange"}]

    

    items.findCustom = (value) =>{

      let results = [];

      items.find((item) => {

      var listKeys = Object.keys(item);

      listKeys.forEach((keyName) => {

        if(item[keyName] == value) 

          results.push(item);

        

    })

    })

    return results;

    }

    console.log(items.findCustom("BANANA")) 


查看完整回答
反對(duì) 回復(fù) 2022-05-14
?
絕地?zé)o雙

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

你可以使用filter方法


let filteredArray = items.filter(item => item.fruits ==='BANANA');

console.log(filteredArray);


更新


對(duì)于更多動(dòng)態(tài)過濾器,您可以創(chuàng)建要查找的字符串列表讓我們說:


let fruitsToFilter = ['BANANA', 'APPLE',...]

然后就可以使用像這樣的過濾方法來實(shí)現(xiàn)動(dòng)態(tài)過濾


let filteredArray = items.filter(item => fruitsToFilter.includes(item.fruits));

console.log(filteredArray);


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

添加回答

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