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

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

從 javascript 文件調用時,函數不會在剃刀頁面上執行

從 javascript 文件調用時,函數不會在剃刀頁面上執行

搖曳的薔薇 2023-05-19 19:44:47
我有兩個 JS 數組。他們是arr1和arr2。我想創建另一個名為arr3. 現在它想要比較arr1并插入所有包含和不包含的arr2元素。arr2arr1例子:arr1:  0:    des: "cont1"    note: "cont1"    pro_code: "XXY"  1:    des: "cont2"    note: "cont2"    pro_code: "NNB"  2:    des: "cont4"    note: "cont4"    pro_code: "QQA"  3:    des: "cont5"    note: "cont5"    pro_code: "GFD"arr2:  0:    des: "cont1"    note: "cont1"    pro_code: "XXY"  1:    des: "cont2"    note: "cont2"    pro_code: "NNB"  2:    des: "cont3"    note: "cont3"    pro_code: "QAS"在arr2包含pro_code: QAS. 但它不在arr1。所以它應該包含在arr3.在arr1包含arr1[4] pro_code: GFD. 它should not包含在arr3. Becaseaar1可以包含其他元素。Herearr2必須包含 中的元素arr1。中不能有附加元素arr2。預期輸出:const arr3 = [{  des: "cont3",  note: "cont3",  pro_code: "QAS"}我試過的代碼,不起作用。請幫我解決這個問題。
查看完整描述

3 回答

?
慕哥9229398

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

Set您可以對所有值取 apro_code并通過檢查該值是否不在集合中來過濾第二個數組。


const

? ? array1 = [{ des: "cont1", note: "cont1", pro_code: "XXY" }, { des: "cont2", note: "cont2", pro_code: "NNB" }, { des: "cont4", note: "cont4", pro_code: "QQA" }, { des: "cont5", note: "cont5", pro_code: "GFD" }],

? ? array2 = [{ des: "cont1", note: "cont1", pro_code: "XXY" }, { des: "cont2", note: "cont2", pro_code: "NNB" }, { des: "cont3", note: "cont3", pro_code: "QAS" }],

? ? pro_codes = new Set(array1.map(({ pro_code }) => pro_code)),

? ? result = array2.filter(({ pro_code }) => !pro_codes.has(pro_code));


console.log(result);


查看完整回答
反對 回復 2023-05-19
?
滄海一幻覺

TA貢獻1824條經驗 獲得超5個贊

這是另一個優化的解決方案


const arr1 = [{

  des: "cont1",

  note: "cont1",

  pro_code: "XXY"

}, {

  des: "cont2",

  note: "cont2",

  pro_code: "NNB"

}, {

  des: "cont4",

  note: "cont4",

  pro_code: "QQA"

}, {

  des: "cont5",

  note: "cont5",

  pro_code: "GFD"

}];


const arr2 = [{

  des: "cont1",

  note: "cont1",

  pro_code: "XXY"

}, {

  des: "cont2",

  note: "cont2",

  pro_code: "NNB"

}, {

  des: "cont3",

  note: "cont3",

  pro_code: "QAS"

}];


const results = arr2.filter(({ pro_code: id1 }) => !arr1.some(({ pro_code: id2 }) => id2 === id1));


console.log(results);



查看完整回答
反對 回復 2023-05-19
?
波斯汪

TA貢獻1811條經驗 獲得超4個贊

簡而言之,您的要求是過濾arr2并保留不在arr1.


const arr3 = arr2.filter(function(arr2item) {

  // only keep this item if it is not in arr1

  return !arr1.some(function(arr1item) {

    return arr1item.pro_code === arr2item.pro_code;

  })

});

const arr1 = [{des: "cont1", note: "cont1", pro_code: "XXY"}, {des: "cont2", note: "cont2", pro_code: "NNB"}, {des: "cont4", note: "cont4", pro_code: "QQA"}, {des: "cont5", note: "cont5", pro_code: "GFD"}];

const arr2 = [{des: "cont1", note: "cont1", pro_code: "XXY"}, {des: "cont2", note: "cont2", pro_code: "NNB"}, {des: "cont3", note: "cont3", pro_code: "QAS"}]


const arr3 = arr2.filter(function(arr2item) {

  return !arr1.some(function(arr1item) {

    return arr1item.pro_code === arr2item.pro_code;

  })

});


console.log(arr3);


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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