[英]JavaScript array comparisions and display values from another
I have a case where I need to compare between two arrays and display values from another.;我有一个案例,我需要比较两个 arrays 并显示另一个的值。 For example, I have two arrays:比如我有两个arrays:
let a = ['a','b','c'];
let b = ['textA' 'textB', ' '];
So, I am basically trying to loop over the array b and display value like this:所以,我基本上是在尝试遍历数组 b 并像这样显示值:
textA
textB
C
So, when there are any empty values found in array b, display the same indexed values from array a.因此,当在数组 b 中找到任何空值时,显示数组 a 中的相同索引值。
Can anybody help with this.任何人都可以帮忙吗? Thanks in advance.提前致谢。
you can:你可以:
elem.trim().length
修剪值,看是否有空或只有空格elem.trim().length
(.elem.trim().length && a[index])
如果字符串为空检查其他数组中的数据是否存在 if (.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); } });
One other solution consist to create a result array with array.map and display all key of this new array另一种解决方案包括使用 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.