I'm trying to display multiple elements in a single array. (See example)
But I do not know how to do it ...
Thanks for your help
I display my script:
****Du code******
var color = [];
var risk = [];
if(Operational.length == group.name.length){
color = "green";
risk = "Operational";
console.log(group.name);
// toto
// tata
// titi
}
if(Test.length > 0){
color = "";
risk = "";
}
if(PartialOutage.length > 0){
color = "orange"
risk = "Partial";
}
if(MajorOutage.length > 0){
color = "red";
risk = "Major ";
}
var Group = new Array(color,risk,group.name);
Groups.push(Group);
Groups.sort();
}
});
console.log(Groups);
}
**** Du code *****
Actual result :
[ [ 'green', 'Operational', 'toto' ],
[ 'green', 'Operational', 'tata' ],
[ 'green', 'Operational', 'titi' ],
[ 'orange', 'Partial', 'test' ],
[ 'red', 'Major ', 'test2' ] ]
Expected result :
[ [ 'green', 'Operational', 'toto,tata,titi' ],
[ 'orange', 'Partial', 'test' ],
[ 'red', 'Major ', 'test2' ] ]
check this out.Recreate the final array value as you expected format using Array#reduce
a
b
child array with newly created array a
forloop
for break the loopa
.Then Merge two array and filter the duplicate //Check the comments in snippet
var a = [ ['green', 'Operational', 'toto'], ['green', 'Operational', 'tata'], ['green', 'Operational', 'titi'], ['orange', 'Partial', 'test'], ['red', 'Major ', 'test2'] ]; a = a.reduce(function(a, b) { for (var i = 0; i <= b.length; i++) { var brk = false; var check = true; if (a.length > 0) { a.forEach(function(k, ind) { if (k.indexOf(b[i]) > -1) { //if contains same arg k = k.concat(b); //join two array var j = k.filter((l,n)=> k.filter(m=> m == l).length == 1).filter(o=>o) a[ind] = k.filter((l, n) => k.indexOf(l) != n); // filter duplicate a[ind].push(j.join(',')) brk = true; //then break the statement check = true; } else { check = false; //its not contain any arg direct push child into new array } }) if (brk) { break; } if (!check) { a.push(b) } } else { a.push(b); } } return a // recreated new array return }, []); console.log(a);
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.