[英]How to get values from an array in angular 4
I have 3 arrays:我有 3 个数组:
A = [AC,DC];
B = [3,4,4,4];
c = [1,2,2,2];
I need to create a loop to retrieve the values from these array and display in the format:我需要创建一个循环来从这些数组中检索值并以以下格式显示:
{
name:AC
value:{
one:3 // from B
two :1, // from C
},
{
one:4, // from B
two:2 // from C
}
},
{
name:DC
value:{
one:3 // from B
two :1, // from C
},
{
one:4, // from B
two:2 // from C
}
}
}
The arrays are changing ,not consistent .数组在变化,不一致。
this.A.forEach((item,index)=>{
this.B.forEach((item1,index2)=>{
this.multi.push(
{
"names":item,
"value":[
{
"one":item1,
"two":this.c[index2]
}
]
})
})
i use this for loop , but i did't get the correct format.Any mistake in my code?我用这个 for 循环,但我没有得到正确的格式。我的代码有什么错误吗? anything need to change ?有什么需要改变的吗? Any help will be appreciated.任何帮助将不胜感激。 any other option rather than the for-each loop.任何其他选项而不是 for-each 循环。 I use for each loop for getting all values,because the values in array is changes.And one thing also first array length is not same as second and third array.but array 2 and array 3 will be same.我使用每个循环来获取所有值,因为数组中的值是变化的。还有一件事,第一个数组长度与第二个和第三个数组不同。但数组 2 和数组 3 将相同。
Here's a simple solution:这是一个简单的解决方案:
let A = ["AC","DC"]; let B = [3,4,4,4]; let C = [1,2,2,2]; let result = {}; A.forEach((element) => { result[element] = { name: element, value: B.map((_, index) => { return { one: B[index], two: C[index] }; }) }; }); console.log(result);
use this用这个
let A = ["AC","DC"]; let B = [3,4,4,4].filter((v,i) => [3,4,4,4].indexOf(v) == i); let C = [1,2,2,2].filter((v,i) => [1,2,2,2].indexOf(v) == i); let E = A.map(item=>{ return {'name':item, 'value':B.map((item,index)=>{ return {'one':B[index],'two':C[index]} }) } }) console.log(E)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.