Slick way to create nested array types
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have something like this:
const rhs = ['Foo.Bar', 'Two', 'Three'];
and I want to get:
Array<Foo.Bar<Two<Three>>>
I have this which works:
const literalType = rhs.reduce((a,b) => {
return [a,'<',b].join('');
});
const withBraces = literalType.concat(
new Array(rhs.length).fill(null).join('>')
);
const finalVal = `Array<${withBraces}>`,
But I am looking for something a little slicker than that. Does anyone have a good idea on how to make this simpler?
javascript node.js
add a comment |
I have something like this:
const rhs = ['Foo.Bar', 'Two', 'Three'];
and I want to get:
Array<Foo.Bar<Two<Three>>>
I have this which works:
const literalType = rhs.reduce((a,b) => {
return [a,'<',b].join('');
});
const withBraces = literalType.concat(
new Array(rhs.length).fill(null).join('>')
);
const finalVal = `Array<${withBraces}>`,
But I am looking for something a little slicker than that. Does anyone have a good idea on how to make this simpler?
javascript node.js
add a comment |
I have something like this:
const rhs = ['Foo.Bar', 'Two', 'Three'];
and I want to get:
Array<Foo.Bar<Two<Three>>>
I have this which works:
const literalType = rhs.reduce((a,b) => {
return [a,'<',b].join('');
});
const withBraces = literalType.concat(
new Array(rhs.length).fill(null).join('>')
);
const finalVal = `Array<${withBraces}>`,
But I am looking for something a little slicker than that. Does anyone have a good idea on how to make this simpler?
javascript node.js
I have something like this:
const rhs = ['Foo.Bar', 'Two', 'Three'];
and I want to get:
Array<Foo.Bar<Two<Three>>>
I have this which works:
const literalType = rhs.reduce((a,b) => {
return [a,'<',b].join('');
});
const withBraces = literalType.concat(
new Array(rhs.length).fill(null).join('>')
);
const finalVal = `Array<${withBraces}>`,
But I am looking for something a little slicker than that. Does anyone have a good idea on how to make this simpler?
javascript node.js
javascript node.js
asked Nov 17 '18 at 4:46
MrCholoMrCholo
1,6231036
1,6231036
add a comment |
add a comment |
6 Answers
6
active
oldest
votes
Combining two of the other answers, you can use reduceRight()
and template literals to get something "slick":
const rhs = ['Foo.Bar', 'Two', 'Three'];
const finalVal = `Array${rhs.reduceRight((a, b) => `<${b}${a}>`, '')}`;
console.log(finalVal);
add a comment |
You can use "Array.reduce" like below to achieve this
const rhs = ['Foo.Bar', 'Two', 'Three'];
let res = rhs.reduce((s, d, i) =>
(s += '<' + d
, i == rhs.length -1 && (s += ('>'.repeat(i+1)))
, s)
, 'Array')
console.log(res)
add a comment |
You can use reduceRight
which helps in going "outwards" in this case:
const rhs = ['Foo.Bar', 'Two', 'Three'];
const res = 'Array' + rhs.reduceRight((a, c) => `<${c + a}>`, '');
console.log(res);
add a comment |
I just reverse the array and build from the inside instead of the outside, then it's easier, see:
const literalType = rhs.reverse().reduce((a,b) => {
return [b,'<',a, '>'].join('');
});
const finalVal = `Array<${literalType}>`,
1
reduceRight
is also a function, and won't modify the original array.
– jhpratt
Nov 17 '18 at 5:39
add a comment |
You can do something like this:
const rhs = ['Foo.Bar', 'Two', 'Three'];
const r = `Array<${rhs.reduce((r,c,i,a) =>
`${r}<${c}`.concat(i == a.length-1 ? '>'.repeat(i) : ''))}>`
console.log(r)
The idea is to use the ES6 template literal hydration with Array.reduce
since that method provides convenient access to the array index
and the actual array
internally.
add a comment |
We can also use simple constructs like,
str = "Array<" + rhs.join("<") + (">".repeat(rhs.length))
console.log(str)
Alternately, for empty array we can prepend first part "Array<" conditionally
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%2f53348307%2fslick-way-to-create-nested-array-types%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
Combining two of the other answers, you can use reduceRight()
and template literals to get something "slick":
const rhs = ['Foo.Bar', 'Two', 'Three'];
const finalVal = `Array${rhs.reduceRight((a, b) => `<${b}${a}>`, '')}`;
console.log(finalVal);
add a comment |
Combining two of the other answers, you can use reduceRight()
and template literals to get something "slick":
const rhs = ['Foo.Bar', 'Two', 'Three'];
const finalVal = `Array${rhs.reduceRight((a, b) => `<${b}${a}>`, '')}`;
console.log(finalVal);
add a comment |
Combining two of the other answers, you can use reduceRight()
and template literals to get something "slick":
const rhs = ['Foo.Bar', 'Two', 'Three'];
const finalVal = `Array${rhs.reduceRight((a, b) => `<${b}${a}>`, '')}`;
console.log(finalVal);
Combining two of the other answers, you can use reduceRight()
and template literals to get something "slick":
const rhs = ['Foo.Bar', 'Two', 'Three'];
const finalVal = `Array${rhs.reduceRight((a, b) => `<${b}${a}>`, '')}`;
console.log(finalVal);
const rhs = ['Foo.Bar', 'Two', 'Three'];
const finalVal = `Array${rhs.reduceRight((a, b) => `<${b}${a}>`, '')}`;
console.log(finalVal);
const rhs = ['Foo.Bar', 'Two', 'Three'];
const finalVal = `Array${rhs.reduceRight((a, b) => `<${b}${a}>`, '')}`;
console.log(finalVal);
answered Nov 17 '18 at 7:41
Patrick RobertsPatrick Roberts
21.3k33878
21.3k33878
add a comment |
add a comment |
You can use "Array.reduce" like below to achieve this
const rhs = ['Foo.Bar', 'Two', 'Three'];
let res = rhs.reduce((s, d, i) =>
(s += '<' + d
, i == rhs.length -1 && (s += ('>'.repeat(i+1)))
, s)
, 'Array')
console.log(res)
add a comment |
You can use "Array.reduce" like below to achieve this
const rhs = ['Foo.Bar', 'Two', 'Three'];
let res = rhs.reduce((s, d, i) =>
(s += '<' + d
, i == rhs.length -1 && (s += ('>'.repeat(i+1)))
, s)
, 'Array')
console.log(res)
add a comment |
You can use "Array.reduce" like below to achieve this
const rhs = ['Foo.Bar', 'Two', 'Three'];
let res = rhs.reduce((s, d, i) =>
(s += '<' + d
, i == rhs.length -1 && (s += ('>'.repeat(i+1)))
, s)
, 'Array')
console.log(res)
You can use "Array.reduce" like below to achieve this
const rhs = ['Foo.Bar', 'Two', 'Three'];
let res = rhs.reduce((s, d, i) =>
(s += '<' + d
, i == rhs.length -1 && (s += ('>'.repeat(i+1)))
, s)
, 'Array')
console.log(res)
const rhs = ['Foo.Bar', 'Two', 'Three'];
let res = rhs.reduce((s, d, i) =>
(s += '<' + d
, i == rhs.length -1 && (s += ('>'.repeat(i+1)))
, s)
, 'Array')
console.log(res)
const rhs = ['Foo.Bar', 'Two', 'Three'];
let res = rhs.reduce((s, d, i) =>
(s += '<' + d
, i == rhs.length -1 && (s += ('>'.repeat(i+1)))
, s)
, 'Array')
console.log(res)
answered Nov 17 '18 at 4:55
Nitish NarangNitish Narang
2,9801815
2,9801815
add a comment |
add a comment |
You can use reduceRight
which helps in going "outwards" in this case:
const rhs = ['Foo.Bar', 'Two', 'Three'];
const res = 'Array' + rhs.reduceRight((a, c) => `<${c + a}>`, '');
console.log(res);
add a comment |
You can use reduceRight
which helps in going "outwards" in this case:
const rhs = ['Foo.Bar', 'Two', 'Three'];
const res = 'Array' + rhs.reduceRight((a, c) => `<${c + a}>`, '');
console.log(res);
add a comment |
You can use reduceRight
which helps in going "outwards" in this case:
const rhs = ['Foo.Bar', 'Two', 'Three'];
const res = 'Array' + rhs.reduceRight((a, c) => `<${c + a}>`, '');
console.log(res);
You can use reduceRight
which helps in going "outwards" in this case:
const rhs = ['Foo.Bar', 'Two', 'Three'];
const res = 'Array' + rhs.reduceRight((a, c) => `<${c + a}>`, '');
console.log(res);
const rhs = ['Foo.Bar', 'Two', 'Three'];
const res = 'Array' + rhs.reduceRight((a, c) => `<${c + a}>`, '');
console.log(res);
const rhs = ['Foo.Bar', 'Two', 'Three'];
const res = 'Array' + rhs.reduceRight((a, c) => `<${c + a}>`, '');
console.log(res);
answered Nov 17 '18 at 5:18
sliderslider
8,56811331
8,56811331
add a comment |
add a comment |
I just reverse the array and build from the inside instead of the outside, then it's easier, see:
const literalType = rhs.reverse().reduce((a,b) => {
return [b,'<',a, '>'].join('');
});
const finalVal = `Array<${literalType}>`,
1
reduceRight
is also a function, and won't modify the original array.
– jhpratt
Nov 17 '18 at 5:39
add a comment |
I just reverse the array and build from the inside instead of the outside, then it's easier, see:
const literalType = rhs.reverse().reduce((a,b) => {
return [b,'<',a, '>'].join('');
});
const finalVal = `Array<${literalType}>`,
1
reduceRight
is also a function, and won't modify the original array.
– jhpratt
Nov 17 '18 at 5:39
add a comment |
I just reverse the array and build from the inside instead of the outside, then it's easier, see:
const literalType = rhs.reverse().reduce((a,b) => {
return [b,'<',a, '>'].join('');
});
const finalVal = `Array<${literalType}>`,
I just reverse the array and build from the inside instead of the outside, then it's easier, see:
const literalType = rhs.reverse().reduce((a,b) => {
return [b,'<',a, '>'].join('');
});
const finalVal = `Array<${literalType}>`,
edited Nov 17 '18 at 4:50
Nick Parsons
10.5k21028
10.5k21028
answered Nov 17 '18 at 4:48
MrCholoMrCholo
1,6231036
1,6231036
1
reduceRight
is also a function, and won't modify the original array.
– jhpratt
Nov 17 '18 at 5:39
add a comment |
1
reduceRight
is also a function, and won't modify the original array.
– jhpratt
Nov 17 '18 at 5:39
1
1
reduceRight
is also a function, and won't modify the original array.– jhpratt
Nov 17 '18 at 5:39
reduceRight
is also a function, and won't modify the original array.– jhpratt
Nov 17 '18 at 5:39
add a comment |
You can do something like this:
const rhs = ['Foo.Bar', 'Two', 'Three'];
const r = `Array<${rhs.reduce((r,c,i,a) =>
`${r}<${c}`.concat(i == a.length-1 ? '>'.repeat(i) : ''))}>`
console.log(r)
The idea is to use the ES6 template literal hydration with Array.reduce
since that method provides convenient access to the array index
and the actual array
internally.
add a comment |
You can do something like this:
const rhs = ['Foo.Bar', 'Two', 'Three'];
const r = `Array<${rhs.reduce((r,c,i,a) =>
`${r}<${c}`.concat(i == a.length-1 ? '>'.repeat(i) : ''))}>`
console.log(r)
The idea is to use the ES6 template literal hydration with Array.reduce
since that method provides convenient access to the array index
and the actual array
internally.
add a comment |
You can do something like this:
const rhs = ['Foo.Bar', 'Two', 'Three'];
const r = `Array<${rhs.reduce((r,c,i,a) =>
`${r}<${c}`.concat(i == a.length-1 ? '>'.repeat(i) : ''))}>`
console.log(r)
The idea is to use the ES6 template literal hydration with Array.reduce
since that method provides convenient access to the array index
and the actual array
internally.
You can do something like this:
const rhs = ['Foo.Bar', 'Two', 'Three'];
const r = `Array<${rhs.reduce((r,c,i,a) =>
`${r}<${c}`.concat(i == a.length-1 ? '>'.repeat(i) : ''))}>`
console.log(r)
The idea is to use the ES6 template literal hydration with Array.reduce
since that method provides convenient access to the array index
and the actual array
internally.
const rhs = ['Foo.Bar', 'Two', 'Three'];
const r = `Array<${rhs.reduce((r,c,i,a) =>
`${r}<${c}`.concat(i == a.length-1 ? '>'.repeat(i) : ''))}>`
console.log(r)
const rhs = ['Foo.Bar', 'Two', 'Three'];
const r = `Array<${rhs.reduce((r,c,i,a) =>
`${r}<${c}`.concat(i == a.length-1 ? '>'.repeat(i) : ''))}>`
console.log(r)
edited Nov 17 '18 at 5:16
answered Nov 17 '18 at 5:11
AkrionAkrion
9,65011426
9,65011426
add a comment |
add a comment |
We can also use simple constructs like,
str = "Array<" + rhs.join("<") + (">".repeat(rhs.length))
console.log(str)
Alternately, for empty array we can prepend first part "Array<" conditionally
add a comment |
We can also use simple constructs like,
str = "Array<" + rhs.join("<") + (">".repeat(rhs.length))
console.log(str)
Alternately, for empty array we can prepend first part "Array<" conditionally
add a comment |
We can also use simple constructs like,
str = "Array<" + rhs.join("<") + (">".repeat(rhs.length))
console.log(str)
Alternately, for empty array we can prepend first part "Array<" conditionally
We can also use simple constructs like,
str = "Array<" + rhs.join("<") + (">".repeat(rhs.length))
console.log(str)
Alternately, for empty array we can prepend first part "Array<" conditionally
answered Nov 17 '18 at 7:14
MukundMukund
31117
31117
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%2f53348307%2fslick-way-to-create-nested-array-types%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