How to display the array?
I have 3 arrays:
Array1
and Array2
have connections to each other:
var Array1 = ['Bob','James','Kanye','West'];
var Array2 = [0,1,2,3];
var Array3 = [1,3,0,2];
How do I display it to this?
Array4 = ['James', 'West', 'Bob','Kanye'];
你将需要2个循环,1将遍历Array3的每个元素,第二个循环将用于查找索引值将与Array2进行比较,以查找数组1的索引,然后该索引值将从Array1保存在Array4中
for (var i = 0; i < Array3.length; i++)
{
var index = Array3[i];
var position=-1;
for(var j=0; j < Array2.length;j++)
{
if(index==Array2[j])
{
position = j;
break;
}
}
Array4[i] = Array1[j];
}
你需要在Array上运行一个循环,把里面的整数作为indexnumber,然后用刚刚从第一个数组中取出的数字打印出第一个数组。
You need to use -- and read the documentation for -- arrays' map
method:
const names = ['Bob','James','Kanye','West'];
const order = [1,3,0,2];
const orderedNames = order.map(x => names[x]);
console.log(orderedNames);
// => ["James", "West", "Bob", "Kanye"]
Fiddle: https://jsfiddle.net/68hrrjx3/
Also kinda relevant in the context of the other answers: What is the difference between declarative and imperative programming
链接地址: http://www.djcxy.com/p/22888.html上一篇: 属性与字段:需要帮助了解字段上属性的用法
下一篇: 如何显示数组?