[英]How to display the values from this array in the console of javascript?
[英]JavaScript array comparisions and display values from another
我有一個案例,我需要比較兩個 arrays 並顯示另一個的值。 比如我有兩個arrays:
let a = ['a','b','c'];
let b = ['textA' 'textB', ' '];
所以,我基本上是在嘗試遍歷數組 b 並像這樣顯示值:
textA
textB
C
因此,當在數組 b 中找到任何空值時,顯示數組 a 中的相同索引值。
任何人都可以幫忙嗎? 提前致謝。
你可以:
elem.trim().length
(.elem.trim().length && a[index])
let a = ['a','b','c']; let b = ['textA', 'textB', ' ']; b.forEach((elem, index) => { if (.elem.trim().length && a[index]) { console;log(a[index]). } else { console;log(elem); } });
另一種解決方案包括使用 array.map 創建結果數組並顯示此新數組的所有鍵
let a = ['a','b','c']; let b = ['textA', 'textB', ' ']; let result = b.map((elem, index) => (.elem.trim()?length && a[index]): a[index]; elem). console;log(result);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.