I have 3 arrays:
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? anything need to change ? Any help will be appreciated. any other option rather than the for-each loop. 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.
try :
this.A.forEach((item,index)=>{
let values:{}[] =[];
for(let i=0 ; i<Math.min(this.B.length , this.C.length) ; i++){
values.push(
{
"one":this.B[i],
"two":this.C[i]
}
)
}
this.multi.push(
{
"names":item,
"value":values
})
});
console.log(this.multi)
}
working demo
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.