亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何循環遍歷 reactjs 上的對象數組,然后根據條件刪除對象

如何循環遍歷 reactjs 上的對象數組,然后根據條件刪除對象

大話西游666 2023-06-09 10:41:38
我正在使用reactjs下面的數據,我想遍歷數組然后if key === "oneline" or key === "twoline"刪除整個對象。在這個例子中,在移除兩個對象(轉換)之后,根據上面的描述,它只會返回數組中0和內部的對象。3const data = [    {0: {key: "zeroline", door: "zero"}},    {1: {key: "oneline", door: "one"}},    {2: {key: "twoline", door: "two"}},    {3: {key: "threeline", door: "three"}}, ]任何幫助都會非常有幫助。
查看完整描述

3 回答

?
慕勒3428872

TA貢獻1848條經驗 獲得超6個贊

使用數組過濾方法。filter() 方法創建一個數組,其中填充了所有通過測試的數組元素。數組過濾器的語法:array.filter(function(currentValue, index, arr), thisValue)


const data = [

  { 0: { key: "zeroline", door: "zero" } },

  { 1: { key: "oneline", door: "one" } },

  { 2: { key: "twoline", door: "two" } },

  { 3: { key: "threeline", door: "three" } },

];


let ret = data.filter((x, i) => x[i].key !== "oneline" && x[i].key !== "twoline");

console.log(ret);


查看完整回答
反對 回復 2023-06-09
?
慕的地6264312

TA貢獻1817條經驗 獲得超6個贊

一種方法是:


const result = Object.keys(data).reduce((filtered,key,index) => {

   //Getting specific object of array

   //Example for first: data[0]["0"] === {key: "zeroline",door "zero"}

   const object = data[index][key];

   

   //Check if object key is zeroline or twoline

   if(object.key === "zeroline" || object.key === "twoline"){

     //If it is we push object to array

     filtered.push(object)

    }

   return filtered

},[])


查看完整回答
反對 回復 2023-06-09
?
慕尼黑的夜晚無繁華

TA貢獻1864條經驗 獲得超6個贊

你可以用過濾掉它Array#filter。


const keywords = ['twoline', 'oneline'], data = [{0: {key: "zeroline", door: "zero"}},{1: {key: "oneline", door: "one"}},{2: {key: "twoline", door: "two"}},{3: {key: "threeline", door: "three"}}];


const res = data.filter((v, i) => keywords.indexOf(v[i].key) === -1);


console.log(res);


查看完整回答
反對 回復 2023-06-09
  • 3 回答
  • 0 關注
  • 269 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號