簡體   English   中英

下划線刪除值而不替換原始變量

[英]Underscore Remove the Value without replace original variable

我有一個對象數組,我試圖在對象中查找值,然后刪除已存在對象中找到的值。 例如,

當前的JSON對象:

exist=[{"x":6811,"y":15551,"a":["aa","ab","ac"]},{"x":6811,"y":15552,"a":["bb"]},{"x":6812,"y":15551,"a":["aa","cc"]}]

我想找到值為aa的“ a”鍵

最后的結果是

exist= [{"x":6811,"y":15552,"a":["bb"]}]
found= [{"x":6811,"y":15551,"a":["aa","ab","ac"]},{"x":6812,"y":15551,"a":["aa","cc"]}]

您可以在不下划線的情況下進行操作:

 let exist= [ { x: 6811, y: 15551, a: ['aa', 'ab', 'ac'] }, { x: 6811, y: 15552, a: ['aa', 'bb'] }, { x: 6812, y: 15551, a: ['cc'] }, ]; const found = exist.filter(({ a }) => a.includes('aa')); exist = exist.filter(({ a }) => !a.includes('aa')); console.log('found:', found); console.log('exist:', exist); 

您可以將.reject .contains_.result組合

 exist=[ {"x":6811,"y":15551,"a":["aa","ab","ac"]},{"x":6811,"y":15552,"a":["bb"]},{"x":6812,"y":15551,"a":["aa","cc"]}, {"x":6812,"y":15551} ] exist = _.reject(exist, function(item){ return _.contains(_.result(item, "a"), "aa"); }) console.log(exist); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM