I have and array added_collections and current state is:
added_collections
(2) [Array(2), Array(1)]
0: Array(2)
0: []
1: [{…}]
length: 2
[[Prototype]]: Array(0)
1: [Array(10)]
length: 2
I need to remove an product from collection so I write:
added_collections[0][1].splice(0, 1);
so the result I expect to get is :
added_collections
(2) [Array(2), Array(1)]
0: Array(2)
0: []
1: []
length: 2
[[Prototype]]: Array(0)
1: [Array(10)]
length: 2
[[Prototype]]: Array(0)
but instead that I get:
(2) [Array(2), Array(1)]
0: Array(0)
length: 0
[[Prototype]]: Array(0)
1: [Array(10)]
length: 2
Why its two empty arrays are merged?
When I follow steps in browser console everything is fine but when I run the same code to execute its not the same results and seems that 2 empty arrays missing
UPDATE:
this is the full code of added_collection[0]:
'[[[],[{"id":32620843270280,"product_id":4632299831432,"title":"3 / red","price":"249.00","sku":"DM-03-red-3","position":1,"inventory_policy":"deny","compare_at_price":"0.00","fulfillment_service":"manual"}]]]'
and I run js code:
let coll_no = 0;
let page_no = 1;
let product_no = 0;
added_collections[coll_no][page_no].splice(product_no, 1);
Works fine for me
const collection = [
[
[],
[{
"id": 32620843270280,
"product_id": 4632299831432,
"title": "3 / red",
"price": "249.00",
"sku": "DM-03-red-3",
"position": 1,
"inventory_policy": "deny",
"compare_at_price": "0.00",
"fulfillment_service": "manual"
}]
]
]
collection[0][1].splice(0, 1)
console.log(collection)
I have a problem. I have a list of data that I output grouped. Now I want to output the values e.g. erzeugenGes to a barchart. Chart modules itself works. Fail only at the grouped data for evaluation.
Enclosed my code and list
1,2,3,4,5,6,7,8,9,10,11,12 are the Month for the ChartLabels
1.
1: [{…}]
2: [{…}]
3: [{…}]
4: [{…}]
5: [{…}]
6: [{…}]
7: [{…}]
8: [{…}]
9: Array(1)
0: {jahr: 2020, monat: 9, erzeugungGes: 901.04, verbrauchGes: 952.22, eigenverbrauch: 515.24, …}
length: 1
__proto__: Array(0)
10: Array(2)
0:
bezogen: 252.18
eigenverbrauch: 201.62
eingespeist: 140.94
erzeugungGes: 342.53
jahr: 2019
monat: 10
verbrauchGes: 453.75
__proto__: Object
1:
bezogen: 232.07
eigenverbrauch: 174.13
eingespeist: 102.37
erzeugungGes: 276.48
jahr: 2020
monat: 10
verbrauchGes: 406.19
__proto__: Object
length: 2
__proto__: Array(0)
11: Array(1)
0: {jahr: 2019, monat: 11, erzeugungGes: 291.87, verbrauchGes: 761.16, eigenverbrauch: 238.38, …}
length: 1
__proto__: Array(0)
12: [{…}]
/// and Data
getData() {
this.ds.getAuswertungJahr().subscribe(res => {
this.daten = res;
this.data = this.daten.reduce((r, a) => { console.log("a", a); console.log('r', r); r[a.monat] = [...r[a.monat] || [], a]; return r; }, {}); console.log("group", this.data);
this.barChartData[0].data = [];
for (let i = 0; i < this.data.lenght; i++) {
this.barChartData[0].data.push(this.data[i]);
}
})
}
Thanks
I have a list Array<[string, number]> I want to sort this list By the string as a date ASC
my list is like:
0: (2) ["01/09/2020 23:38", 100]
1: (2) ["01/09/2020 23:48", 89]
2: (2) ["01/09/2020 23:36", 100]
3: (2) ["01/09/2020 23:42", 100]
4: (2) ["01/09/2020 23:52", 78]
5: (2) ["01/09/2020 23:40", 100]
6: (2) ["01/09/2020 23:50", 89]
7: (2) ["01/09/2020 23:46", 100]
8: (2) ["01/09/2020 23:56", 78]
9: (2) ["01/09/2020 23:44", 100]
10: (2) ["01/09/2020 23:54", 78]
Use [].sort((a,b)=>a-b) like below:
var dateData=[["01/09/2020 23:38", 100],["01/09/2020 23:48", 89],["01/09/2020 23:36", 100],["01/09/2020 23:42", 100]];
dateData.sort((a,b)=>new Date([a[0]]) - new Date([b[0]]));
console.log(dateData);
I have the following line of code:
$scope.listDeColaboradoresObject.forEach(item => {
item.listNmAssunto = $scope.relatorioTotalMensagensRespondidasColab
.filter(x => x.nmUsuario == item.nmUsuario)
.map(x => x.nmAssunto);
item.listNmAssunto = $scope.removeDuplicates(item.listNmAssunto);
item.listDtResposta = $scope.relatorioTotalMensagensRespondidasColab
.filter(x => x.nmUsuario == item.nmUsuario)
.map(x => x.dtResposta);
});
that runs through this array:
0: {deTipoAtendimento: "012", nmAssunto: "Cartão extraviado", nmUsuario: "15", dtResposta: "2018", total: 1}
1: {deTipoAtendimento: "012", nmAssunto: "Assunto Novo 012", nmUsuario: "Admin", dtResposta: "2018", total: 2}
2: {deTipoAtendimento: "012", nmAssunto: "Assunto Novo 012", nmUsuario: "Administrador", dtResposta: "2018", total: 1}
3: {deTipoAtendimento: "012", nmAssunto: "Assunto Novo 012", nmUsuario: "Administrador IMB", dtResposta: "2018", total: 3}
4: {deTipoAtendimento: "012", nmAssunto: "Assunto Teste GREAt", nmUsuario: "Administrador IMB", dtResposta: "2018", total: 2}
5: {deTipoAtendimento: "012", nmAssunto: "Thais 23042018", nmUsuario: "Administrador IMB", dtResposta: "2018", total: 2}
6: {deTipoAtendimento: "012", nmAssunto: "teste Alterado2", nmUsuario: "Administrador IMB", dtResposta: "2018", total: 1}
and that returns me the following array:
0: {nmUsuario: "15", listNmAssunto: Array(1), listDtResposta: Array(1), $$hashKey: "object:2975"}
1: {nmUsuario: "Admin", listNmAssunto: Array(1), listDtResposta: Array(1), $$hashKey: "object:2976"}
2: {nmUsuario: "Administrador", listNmAssunto: Array(1), listDtResposta: Array(1), $$hashKey: "object:2977"}
3: {nmUsuario: "Administrador IMB", listNmAssunto: Array(4), listDtResposta: Array(4), $$hashKey: "object:2978"}
My question is how do I at the time of the map insert both the nmSubject and the dtResponse within the same array
Why you don’t map into one object?
Like this:
$scope.listDeColaboradoresObject.forEach(item => {
item.listAssunto = $scope.relatorioTotalMensagensRespondidasColab
.filter(x => x.nmUsuario == item.nmUsuario)
.map(x => {a:x.nmAssunto,b: x.dtResposta});
item.listNmAssunto = $scope.removeDuplicates(item.listAssunto);
});
The code you've will just update the existing array. I'm not sure how it creates the output you've posted. But, looking at your output, I'm assuming you want to group based on the nmUsuario and create an array of objects with 2 array properties. You can do the following using reduce, destructiring and Object.values
const input = [{deTipoAtendimento:"012",nmAssunto:"Cartão extraviado",nmUsuario:"15",dtResposta:"2018",total:1},{deTipoAtendimento:"012",nmAssunto:"Assunto Novo 012",nmUsuario:"Admin",dtResposta:"2018",total:2},{deTipoAtendimento:"012",nmAssunto:"Assunto Novo 012",nmUsuario:"Administrador",dtResposta:"2018",total:1},{deTipoAtendimento:"012",nmAssunto:"Assunto Novo 012",nmUsuario:"Administrador IMB",dtResposta:"2018",total:3},{deTipoAtendimento:"012",nmAssunto:"Assunto Teste GREAt",nmUsuario:"Administrador IMB",dtResposta:"2018",total:2},{deTipoAtendimento:"012",nmAssunto:"Thais 23042018",nmUsuario:"Administrador IMB",dtResposta:"2018",total:2},{deTipoAtendimento:"012",nmAssunto:"teste Alterado2",nmUsuario:"Administrador IMB",dtResposta:"2018",total:1}]
const merged = input.reduce((r,{ nmUsuario, nmAssunto, dtResposta }) => {
r[nmUsuario] = r[nmUsuario] || {nmUsuario, listNmAssunto: [], listDtResposta:[]};
r[nmUsuario].listNmAssunto.push(nmAssunto);
r[nmUsuario].listDtResposta.push(dtResposta);
return r;
},{})
const output = Object.values(merged);
console.log(output)
As per your results comment, it looks like you want to group your list by nmUsuario, but create a single array of objects that hold the relevant nmAssunto and dtResposta. You can achieve this with reduce(). Basically, you create an object with an empty array for each nmAssunto, and push a new object into the correct array each iteration. Then use Object.values() to convert to the final output array.
const data = [
{deTipoAtendimento:"012",nmAssunto:"Cartão extraviado",nmUsuario:"15",dtResposta:"2018",total:1},
{deTipoAtendimento:"012",nmAssunto:"Assunto Novo 012",nmUsuario:"Admin",dtResposta:"2018",total:2},
{deTipoAtendimento:"012",nmAssunto:"Assunto Novo 012",nmUsuario:"Administrador",dtResposta:"2018",total:1},{deTipoAtendimento:"012",nmAssunto:"Assunto Novo 012",nmUsuario:"Administrador IMB",dtResposta:"2018",total:3},
{deTipoAtendimento:"012",nmAssunto:"Assunto Teste GREAt",nmUsuario:"Administrador IMB",dtResposta:"2018",total:2},
{deTipoAtendimento:"012",nmAssunto:"Thais 23042018",nmUsuario:"Administrador IMB",dtResposta:"2018",total:2},
{deTipoAtendimento:"012",nmAssunto:"teste Alterado2",nmUsuario:"Administrador IMB",dtResposta:"2018",total:1}
]
const groups = data.reduce((r, { nmUsuario, nmAssunto, dtResposta }) => {
r[nmUsuario] = r[nmUsuario] || {
nmUsuario,
listAssunto: []
}
r[nmUsuario].listAssunto.push({
a: nmAssunto,
b: dtResposta
})
return r
}, {})
console.log(Object.values(groups))
I have a switch in javascript that I need to put it different.
How can I write this in a better way to avoid the switch?
var types = {
Int: 2,
Short: 3,
Long: 4,
Double: 5,
Decimal: 6,
String: 1,
Guid: 10,
Variant: 11,
};
switch (data.columnTypeId) {
case types.String:
case types.Guid:
case types.Variant:
self.GetStrings(data);
break;
case types.Int:
case types.Decimal:
case types.Short:
case types.Long:
case types.Double:
self.GetNumbers(data);
break;
default:
}
What about doing the same map, but map a function reference instead?
var columnTypes = {
2: self.GetNumbers,
3: self.GetNumbers,
4: self.GetNumbers,
5: self.GetNumbers,
6: self.GetNumbers,
1: self.GetStrings,
10: self.GetStrings,
11: self.GetStrings,
0: self.GetDefault //Guessing the default value would be 0
};
columnTypes[data.columnTypeId](data);
or with a safe check:
if (columnTypes.indexOf(data.columnTypeId) !== -1) {
columnTypes[data.columnTypeId](data);
} else {
self.GetDefault(data);
}
Or as #dandavis pointed out in a comment:
columnTypes[data.columnTypeId || self.getDefault](data);