简体   繁体   English

JavaScript 数组比较和显示来自另一个的值

[英]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:你可以:

  • trim the value to see if there is empty or only space elem.trim().length修剪值,看是否有空或只有空格elem.trim().length
  • if string is empty check if data in other array exist if (.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.

相关问题 如何在javascript的控制台中显示这个数组中的值? - How to display the values from this array in the console of javascript? 如何从数组JavaScript显示复选框值 - How to display checkbox values from an array javascript 如何显示 JavaScript 数组中的多个值 - How to display multiple values from JavaScript array 使用JavaScript将另一个数组中的硬编码值替换为另一个数组中的值? - Replace hard coded values in an array with values from another array with JavaScript? 将 object 数组中的值替换为 JavaScript 中另一个数组中的值 - Replace values in array of object with values from another array in JavaScript 如何将数组值从一个数组映射到另一个数组JavaScript? - How to Map Array values from one Array to Another Array JavaScript? 如何从JavaScript获取数组值并在php中显示相同的值 - How to GET array values from JavaScript and display the same values in php Javascript - Array - 将值从一个数组分配给另一个数组中的字符串 - Javascript - Array - Assigning values from one array to strings in another array 根据 JavaScript 中的另一个数组值从数组中选择元素 - Selecting elements from an array based on another array values in JavaScript 选择字段中要显示在另一个文本字段中的值的总和(javascript) - Sum of values from select fields to display in another text field (javascript)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM