Multidimensional array of an unknown size in JavaScript
Really struggling to get my head around this...
I'm trying to sort items from an array into another array, but categorised. My code is currently working, but the trouble is that I need to know the size of the "sort" array. At the moment, there are four entries, which works with "data". The first two data.cat are compared to n, and their IDs are pushed into sort[0]. When two do not match, 1 is added to c. The code then loops through again, pushing the new matches into sort[1], etc, etc.
var data = [
{"cat": 0, "id":"AAAA"},
{"cat": 0, "id":"BBBB"},
{"cat": 1, "id":"CCCC"},
{"cat": 1, "id":"DDDD"},
{"cat": 1, "id":"EEEE"},
{"cat": 1, "id":"FFFF"}, //pseudodata
{"cat": 1, "id":"GGGG"},
{"cat": 2, "id":"HHHH"},
{"cat": 2, "id":"IIII"},
{"cat": 2, "id":"JJJJ"},
{"cat": 3, "id":"KKKK"}
];
var sort = [, , , ,]
function sortDates(){
var n = 0;
for (var i = 0; i < data.length; i++){
if (data[i].cat == n){
console.log("Category " + n +" entry: " + data[i].id);
sort[n].push(data[i].id);
} else {
console.log("Entry " + data[i].id + " does not match. Moving to next category...");
n++;
i--;
}
}
}
This all works okay, but if I add more to the data array (such as {"cat": 4, "id":"LLLL"}), the program crashes with "sort[n] is not a function". This is because there are only four available items in the sort array.
So I'm just wondering, is there any way to get around this? If the data array is always changing size, and more entries/categories are added, do I have to keep resizing the sort array manually?
javascript arrays loops multidimensional-array 2d
add a comment |
Really struggling to get my head around this...
I'm trying to sort items from an array into another array, but categorised. My code is currently working, but the trouble is that I need to know the size of the "sort" array. At the moment, there are four entries, which works with "data". The first two data.cat are compared to n, and their IDs are pushed into sort[0]. When two do not match, 1 is added to c. The code then loops through again, pushing the new matches into sort[1], etc, etc.
var data = [
{"cat": 0, "id":"AAAA"},
{"cat": 0, "id":"BBBB"},
{"cat": 1, "id":"CCCC"},
{"cat": 1, "id":"DDDD"},
{"cat": 1, "id":"EEEE"},
{"cat": 1, "id":"FFFF"}, //pseudodata
{"cat": 1, "id":"GGGG"},
{"cat": 2, "id":"HHHH"},
{"cat": 2, "id":"IIII"},
{"cat": 2, "id":"JJJJ"},
{"cat": 3, "id":"KKKK"}
];
var sort = [, , , ,]
function sortDates(){
var n = 0;
for (var i = 0; i < data.length; i++){
if (data[i].cat == n){
console.log("Category " + n +" entry: " + data[i].id);
sort[n].push(data[i].id);
} else {
console.log("Entry " + data[i].id + " does not match. Moving to next category...");
n++;
i--;
}
}
}
This all works okay, but if I add more to the data array (such as {"cat": 4, "id":"LLLL"}), the program crashes with "sort[n] is not a function". This is because there are only four available items in the sort array.
So I'm just wondering, is there any way to get around this? If the data array is always changing size, and more entries/categories are added, do I have to keep resizing the sort array manually?
javascript arrays loops multidimensional-array 2d
add a comment |
Really struggling to get my head around this...
I'm trying to sort items from an array into another array, but categorised. My code is currently working, but the trouble is that I need to know the size of the "sort" array. At the moment, there are four entries, which works with "data". The first two data.cat are compared to n, and their IDs are pushed into sort[0]. When two do not match, 1 is added to c. The code then loops through again, pushing the new matches into sort[1], etc, etc.
var data = [
{"cat": 0, "id":"AAAA"},
{"cat": 0, "id":"BBBB"},
{"cat": 1, "id":"CCCC"},
{"cat": 1, "id":"DDDD"},
{"cat": 1, "id":"EEEE"},
{"cat": 1, "id":"FFFF"}, //pseudodata
{"cat": 1, "id":"GGGG"},
{"cat": 2, "id":"HHHH"},
{"cat": 2, "id":"IIII"},
{"cat": 2, "id":"JJJJ"},
{"cat": 3, "id":"KKKK"}
];
var sort = [, , , ,]
function sortDates(){
var n = 0;
for (var i = 0; i < data.length; i++){
if (data[i].cat == n){
console.log("Category " + n +" entry: " + data[i].id);
sort[n].push(data[i].id);
} else {
console.log("Entry " + data[i].id + " does not match. Moving to next category...");
n++;
i--;
}
}
}
This all works okay, but if I add more to the data array (such as {"cat": 4, "id":"LLLL"}), the program crashes with "sort[n] is not a function". This is because there are only four available items in the sort array.
So I'm just wondering, is there any way to get around this? If the data array is always changing size, and more entries/categories are added, do I have to keep resizing the sort array manually?
javascript arrays loops multidimensional-array 2d
Really struggling to get my head around this...
I'm trying to sort items from an array into another array, but categorised. My code is currently working, but the trouble is that I need to know the size of the "sort" array. At the moment, there are four entries, which works with "data". The first two data.cat are compared to n, and their IDs are pushed into sort[0]. When two do not match, 1 is added to c. The code then loops through again, pushing the new matches into sort[1], etc, etc.
var data = [
{"cat": 0, "id":"AAAA"},
{"cat": 0, "id":"BBBB"},
{"cat": 1, "id":"CCCC"},
{"cat": 1, "id":"DDDD"},
{"cat": 1, "id":"EEEE"},
{"cat": 1, "id":"FFFF"}, //pseudodata
{"cat": 1, "id":"GGGG"},
{"cat": 2, "id":"HHHH"},
{"cat": 2, "id":"IIII"},
{"cat": 2, "id":"JJJJ"},
{"cat": 3, "id":"KKKK"}
];
var sort = [, , , ,]
function sortDates(){
var n = 0;
for (var i = 0; i < data.length; i++){
if (data[i].cat == n){
console.log("Category " + n +" entry: " + data[i].id);
sort[n].push(data[i].id);
} else {
console.log("Entry " + data[i].id + " does not match. Moving to next category...");
n++;
i--;
}
}
}
This all works okay, but if I add more to the data array (such as {"cat": 4, "id":"LLLL"}), the program crashes with "sort[n] is not a function". This is because there are only four available items in the sort array.
So I'm just wondering, is there any way to get around this? If the data array is always changing size, and more entries/categories are added, do I have to keep resizing the sort array manually?
javascript arrays loops multidimensional-array 2d
javascript arrays loops multidimensional-array 2d
asked Nov 25 '18 at 19:20
default-LAdefault-LA
3517
3517
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You could use a default check of the array with a logical OR ||
and assign an empty array if the item is not set.
sort[n] = sort[n] || ;
sort[n].push(data[i].id);
BTW, you could use cat
directly without iterating sort
array for getting the right index.
function sortDates() {
var i, sort = ;
for (i = 0; i < data.length; i++) {
sort[data[i].cat] = sort[data[i].cat] || ;
sort[data[i].cat].push(data[i].id);
}
return sort;
}
var data = [{ cat: 0, id: "AAAA" }, { cat: 0, id: "BBBB" }, { cat: 1, id: "CCCC" }, { cat: 1, id: "DDDD" }, { cat: 1, id: "EEEE" }, { cat: 1, id: "FFFF" }, { cat: 1, id: "GGGG" }, { cat: 2, id: "HHHH" }, { cat: 2, id: "IIII" }, { cat: 2, id: "JJJJ" }, { cat: 3, id: "KKKK" }],
result = sortDates(data);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Absolutely perfect - thank you! I knew there was a more efficient way to do this... so the line:sort[data[i].cat] = sort[data[i].cat] || ;
Is this basically saying, pushdata[i]
tosort[data[i].cat]
, or ifsort[data[i].cat
doesn't exist, use the logic OR, which pushes it into a new item insort
?
– default-LA
Nov 25 '18 at 20:38
right first the check and if not a truthy value, like an array, then assign an array and later push the new value.
– Nina Scholz
Nov 25 '18 at 20:41
add a comment |
Another way you can achieve this with ES6 in a more concise manner is to use Array.reduce
and inside Array.sort
via String.localeCompare
:
var data = [{ "cat": 0, "id": "BBBB" }, { "cat": 0, "id": "AAAA" }, { "cat": 1, "id": "CCCC" }, { "cat": 1, "id": "DDDD" }, { "cat": 1, "id": "EEEE" }, { "cat": 1, "id": "FFFF" }, { "cat": 1, "id": "GGGG" }, { "cat": 2, "id": "HHHH" }, { "cat": 2, "id": "IIII" }, { "cat": 2, "id": "JJJJ" }, { "cat": 3, "id": "KKKK" } ]
const result = Object.values(data.reduce((r,c) => {
r[c.cat] = [...r[c.cat] || , c.id].sort((a,b) => a.localeCompare(b))
return r
}, {}))
console.log(result)
String.localeCompare
has various options in terms of how to compare (like dealing with numbers in the strings, case sensitivity etc)
The idea is to use the Array.reduce
is to group the entries in an object and then just use Object.values
to get the desired output (milti-dimentional array).
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53471015%2fmultidimensional-array-of-an-unknown-size-in-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could use a default check of the array with a logical OR ||
and assign an empty array if the item is not set.
sort[n] = sort[n] || ;
sort[n].push(data[i].id);
BTW, you could use cat
directly without iterating sort
array for getting the right index.
function sortDates() {
var i, sort = ;
for (i = 0; i < data.length; i++) {
sort[data[i].cat] = sort[data[i].cat] || ;
sort[data[i].cat].push(data[i].id);
}
return sort;
}
var data = [{ cat: 0, id: "AAAA" }, { cat: 0, id: "BBBB" }, { cat: 1, id: "CCCC" }, { cat: 1, id: "DDDD" }, { cat: 1, id: "EEEE" }, { cat: 1, id: "FFFF" }, { cat: 1, id: "GGGG" }, { cat: 2, id: "HHHH" }, { cat: 2, id: "IIII" }, { cat: 2, id: "JJJJ" }, { cat: 3, id: "KKKK" }],
result = sortDates(data);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Absolutely perfect - thank you! I knew there was a more efficient way to do this... so the line:sort[data[i].cat] = sort[data[i].cat] || ;
Is this basically saying, pushdata[i]
tosort[data[i].cat]
, or ifsort[data[i].cat
doesn't exist, use the logic OR, which pushes it into a new item insort
?
– default-LA
Nov 25 '18 at 20:38
right first the check and if not a truthy value, like an array, then assign an array and later push the new value.
– Nina Scholz
Nov 25 '18 at 20:41
add a comment |
You could use a default check of the array with a logical OR ||
and assign an empty array if the item is not set.
sort[n] = sort[n] || ;
sort[n].push(data[i].id);
BTW, you could use cat
directly without iterating sort
array for getting the right index.
function sortDates() {
var i, sort = ;
for (i = 0; i < data.length; i++) {
sort[data[i].cat] = sort[data[i].cat] || ;
sort[data[i].cat].push(data[i].id);
}
return sort;
}
var data = [{ cat: 0, id: "AAAA" }, { cat: 0, id: "BBBB" }, { cat: 1, id: "CCCC" }, { cat: 1, id: "DDDD" }, { cat: 1, id: "EEEE" }, { cat: 1, id: "FFFF" }, { cat: 1, id: "GGGG" }, { cat: 2, id: "HHHH" }, { cat: 2, id: "IIII" }, { cat: 2, id: "JJJJ" }, { cat: 3, id: "KKKK" }],
result = sortDates(data);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Absolutely perfect - thank you! I knew there was a more efficient way to do this... so the line:sort[data[i].cat] = sort[data[i].cat] || ;
Is this basically saying, pushdata[i]
tosort[data[i].cat]
, or ifsort[data[i].cat
doesn't exist, use the logic OR, which pushes it into a new item insort
?
– default-LA
Nov 25 '18 at 20:38
right first the check and if not a truthy value, like an array, then assign an array and later push the new value.
– Nina Scholz
Nov 25 '18 at 20:41
add a comment |
You could use a default check of the array with a logical OR ||
and assign an empty array if the item is not set.
sort[n] = sort[n] || ;
sort[n].push(data[i].id);
BTW, you could use cat
directly without iterating sort
array for getting the right index.
function sortDates() {
var i, sort = ;
for (i = 0; i < data.length; i++) {
sort[data[i].cat] = sort[data[i].cat] || ;
sort[data[i].cat].push(data[i].id);
}
return sort;
}
var data = [{ cat: 0, id: "AAAA" }, { cat: 0, id: "BBBB" }, { cat: 1, id: "CCCC" }, { cat: 1, id: "DDDD" }, { cat: 1, id: "EEEE" }, { cat: 1, id: "FFFF" }, { cat: 1, id: "GGGG" }, { cat: 2, id: "HHHH" }, { cat: 2, id: "IIII" }, { cat: 2, id: "JJJJ" }, { cat: 3, id: "KKKK" }],
result = sortDates(data);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
You could use a default check of the array with a logical OR ||
and assign an empty array if the item is not set.
sort[n] = sort[n] || ;
sort[n].push(data[i].id);
BTW, you could use cat
directly without iterating sort
array for getting the right index.
function sortDates() {
var i, sort = ;
for (i = 0; i < data.length; i++) {
sort[data[i].cat] = sort[data[i].cat] || ;
sort[data[i].cat].push(data[i].id);
}
return sort;
}
var data = [{ cat: 0, id: "AAAA" }, { cat: 0, id: "BBBB" }, { cat: 1, id: "CCCC" }, { cat: 1, id: "DDDD" }, { cat: 1, id: "EEEE" }, { cat: 1, id: "FFFF" }, { cat: 1, id: "GGGG" }, { cat: 2, id: "HHHH" }, { cat: 2, id: "IIII" }, { cat: 2, id: "JJJJ" }, { cat: 3, id: "KKKK" }],
result = sortDates(data);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
function sortDates() {
var i, sort = ;
for (i = 0; i < data.length; i++) {
sort[data[i].cat] = sort[data[i].cat] || ;
sort[data[i].cat].push(data[i].id);
}
return sort;
}
var data = [{ cat: 0, id: "AAAA" }, { cat: 0, id: "BBBB" }, { cat: 1, id: "CCCC" }, { cat: 1, id: "DDDD" }, { cat: 1, id: "EEEE" }, { cat: 1, id: "FFFF" }, { cat: 1, id: "GGGG" }, { cat: 2, id: "HHHH" }, { cat: 2, id: "IIII" }, { cat: 2, id: "JJJJ" }, { cat: 3, id: "KKKK" }],
result = sortDates(data);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
function sortDates() {
var i, sort = ;
for (i = 0; i < data.length; i++) {
sort[data[i].cat] = sort[data[i].cat] || ;
sort[data[i].cat].push(data[i].id);
}
return sort;
}
var data = [{ cat: 0, id: "AAAA" }, { cat: 0, id: "BBBB" }, { cat: 1, id: "CCCC" }, { cat: 1, id: "DDDD" }, { cat: 1, id: "EEEE" }, { cat: 1, id: "FFFF" }, { cat: 1, id: "GGGG" }, { cat: 2, id: "HHHH" }, { cat: 2, id: "IIII" }, { cat: 2, id: "JJJJ" }, { cat: 3, id: "KKKK" }],
result = sortDates(data);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
edited Nov 25 '18 at 19:28
answered Nov 25 '18 at 19:23
Nina ScholzNina Scholz
191k15104176
191k15104176
Absolutely perfect - thank you! I knew there was a more efficient way to do this... so the line:sort[data[i].cat] = sort[data[i].cat] || ;
Is this basically saying, pushdata[i]
tosort[data[i].cat]
, or ifsort[data[i].cat
doesn't exist, use the logic OR, which pushes it into a new item insort
?
– default-LA
Nov 25 '18 at 20:38
right first the check and if not a truthy value, like an array, then assign an array and later push the new value.
– Nina Scholz
Nov 25 '18 at 20:41
add a comment |
Absolutely perfect - thank you! I knew there was a more efficient way to do this... so the line:sort[data[i].cat] = sort[data[i].cat] || ;
Is this basically saying, pushdata[i]
tosort[data[i].cat]
, or ifsort[data[i].cat
doesn't exist, use the logic OR, which pushes it into a new item insort
?
– default-LA
Nov 25 '18 at 20:38
right first the check and if not a truthy value, like an array, then assign an array and later push the new value.
– Nina Scholz
Nov 25 '18 at 20:41
Absolutely perfect - thank you! I knew there was a more efficient way to do this... so the line:
sort[data[i].cat] = sort[data[i].cat] || ;
Is this basically saying, push data[i]
to sort[data[i].cat]
, or if sort[data[i].cat
doesn't exist, use the logic OR, which pushes it into a new item in sort
?– default-LA
Nov 25 '18 at 20:38
Absolutely perfect - thank you! I knew there was a more efficient way to do this... so the line:
sort[data[i].cat] = sort[data[i].cat] || ;
Is this basically saying, push data[i]
to sort[data[i].cat]
, or if sort[data[i].cat
doesn't exist, use the logic OR, which pushes it into a new item in sort
?– default-LA
Nov 25 '18 at 20:38
right first the check and if not a truthy value, like an array, then assign an array and later push the new value.
– Nina Scholz
Nov 25 '18 at 20:41
right first the check and if not a truthy value, like an array, then assign an array and later push the new value.
– Nina Scholz
Nov 25 '18 at 20:41
add a comment |
Another way you can achieve this with ES6 in a more concise manner is to use Array.reduce
and inside Array.sort
via String.localeCompare
:
var data = [{ "cat": 0, "id": "BBBB" }, { "cat": 0, "id": "AAAA" }, { "cat": 1, "id": "CCCC" }, { "cat": 1, "id": "DDDD" }, { "cat": 1, "id": "EEEE" }, { "cat": 1, "id": "FFFF" }, { "cat": 1, "id": "GGGG" }, { "cat": 2, "id": "HHHH" }, { "cat": 2, "id": "IIII" }, { "cat": 2, "id": "JJJJ" }, { "cat": 3, "id": "KKKK" } ]
const result = Object.values(data.reduce((r,c) => {
r[c.cat] = [...r[c.cat] || , c.id].sort((a,b) => a.localeCompare(b))
return r
}, {}))
console.log(result)
String.localeCompare
has various options in terms of how to compare (like dealing with numbers in the strings, case sensitivity etc)
The idea is to use the Array.reduce
is to group the entries in an object and then just use Object.values
to get the desired output (milti-dimentional array).
add a comment |
Another way you can achieve this with ES6 in a more concise manner is to use Array.reduce
and inside Array.sort
via String.localeCompare
:
var data = [{ "cat": 0, "id": "BBBB" }, { "cat": 0, "id": "AAAA" }, { "cat": 1, "id": "CCCC" }, { "cat": 1, "id": "DDDD" }, { "cat": 1, "id": "EEEE" }, { "cat": 1, "id": "FFFF" }, { "cat": 1, "id": "GGGG" }, { "cat": 2, "id": "HHHH" }, { "cat": 2, "id": "IIII" }, { "cat": 2, "id": "JJJJ" }, { "cat": 3, "id": "KKKK" } ]
const result = Object.values(data.reduce((r,c) => {
r[c.cat] = [...r[c.cat] || , c.id].sort((a,b) => a.localeCompare(b))
return r
}, {}))
console.log(result)
String.localeCompare
has various options in terms of how to compare (like dealing with numbers in the strings, case sensitivity etc)
The idea is to use the Array.reduce
is to group the entries in an object and then just use Object.values
to get the desired output (milti-dimentional array).
add a comment |
Another way you can achieve this with ES6 in a more concise manner is to use Array.reduce
and inside Array.sort
via String.localeCompare
:
var data = [{ "cat": 0, "id": "BBBB" }, { "cat": 0, "id": "AAAA" }, { "cat": 1, "id": "CCCC" }, { "cat": 1, "id": "DDDD" }, { "cat": 1, "id": "EEEE" }, { "cat": 1, "id": "FFFF" }, { "cat": 1, "id": "GGGG" }, { "cat": 2, "id": "HHHH" }, { "cat": 2, "id": "IIII" }, { "cat": 2, "id": "JJJJ" }, { "cat": 3, "id": "KKKK" } ]
const result = Object.values(data.reduce((r,c) => {
r[c.cat] = [...r[c.cat] || , c.id].sort((a,b) => a.localeCompare(b))
return r
}, {}))
console.log(result)
String.localeCompare
has various options in terms of how to compare (like dealing with numbers in the strings, case sensitivity etc)
The idea is to use the Array.reduce
is to group the entries in an object and then just use Object.values
to get the desired output (milti-dimentional array).
Another way you can achieve this with ES6 in a more concise manner is to use Array.reduce
and inside Array.sort
via String.localeCompare
:
var data = [{ "cat": 0, "id": "BBBB" }, { "cat": 0, "id": "AAAA" }, { "cat": 1, "id": "CCCC" }, { "cat": 1, "id": "DDDD" }, { "cat": 1, "id": "EEEE" }, { "cat": 1, "id": "FFFF" }, { "cat": 1, "id": "GGGG" }, { "cat": 2, "id": "HHHH" }, { "cat": 2, "id": "IIII" }, { "cat": 2, "id": "JJJJ" }, { "cat": 3, "id": "KKKK" } ]
const result = Object.values(data.reduce((r,c) => {
r[c.cat] = [...r[c.cat] || , c.id].sort((a,b) => a.localeCompare(b))
return r
}, {}))
console.log(result)
String.localeCompare
has various options in terms of how to compare (like dealing with numbers in the strings, case sensitivity etc)
The idea is to use the Array.reduce
is to group the entries in an object and then just use Object.values
to get the desired output (milti-dimentional array).
var data = [{ "cat": 0, "id": "BBBB" }, { "cat": 0, "id": "AAAA" }, { "cat": 1, "id": "CCCC" }, { "cat": 1, "id": "DDDD" }, { "cat": 1, "id": "EEEE" }, { "cat": 1, "id": "FFFF" }, { "cat": 1, "id": "GGGG" }, { "cat": 2, "id": "HHHH" }, { "cat": 2, "id": "IIII" }, { "cat": 2, "id": "JJJJ" }, { "cat": 3, "id": "KKKK" } ]
const result = Object.values(data.reduce((r,c) => {
r[c.cat] = [...r[c.cat] || , c.id].sort((a,b) => a.localeCompare(b))
return r
}, {}))
console.log(result)
var data = [{ "cat": 0, "id": "BBBB" }, { "cat": 0, "id": "AAAA" }, { "cat": 1, "id": "CCCC" }, { "cat": 1, "id": "DDDD" }, { "cat": 1, "id": "EEEE" }, { "cat": 1, "id": "FFFF" }, { "cat": 1, "id": "GGGG" }, { "cat": 2, "id": "HHHH" }, { "cat": 2, "id": "IIII" }, { "cat": 2, "id": "JJJJ" }, { "cat": 3, "id": "KKKK" } ]
const result = Object.values(data.reduce((r,c) => {
r[c.cat] = [...r[c.cat] || , c.id].sort((a,b) => a.localeCompare(b))
return r
}, {}))
console.log(result)
answered Nov 25 '18 at 20:46
AkrionAkrion
9,53011224
9,53011224
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53471015%2fmultidimensional-array-of-an-unknown-size-in-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown