javascript: in a chain, how to get size of previous array?









up vote
2
down vote

favorite












When chaining filter and reduce, how to get the size of the filtered array? I need that size for tailoring responsive design CSS.
My (simplified) code, uses the 3rd and 4th parameters of the callback:



json.articles
.filter(a => keep(a,name))
.reduce((el, a,i,t) =>
i===0? DOMtag('section','class':`media$t.length`)
.appendChild(DOMtag(`article`, content(a,0))).parentElement
: el.appendChild(DOMtag(`article`, content(a,i))).parentElement,
null);


or, even simpler (thanks to luc and his lazy evaluation suggestion below):



json.articles
.filter(a => keep(a,name))
.reduce((el, a,i,t) =>
(el || DOMtag('section','class':`media$t.length`))
.appendChild(DOMtag(`article`, content(a,i))).parentElement,
null);


Both code work, but if someone has an idea about binding this somehow, it would be possible to use the initial value of the accumulator, such as:



json.articles
.filter(a => keep(a,name))
.reduce((el, a) =>
el.appendChild(DOMtag(`article`, content(a,i))).parentElement,
DOMtag('section','class':`media$this.length`));


Any idea?










share|improve this question



















  • 1




    the 4th argument in the reducer function is the source array. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
    – kunal_bohra
    Nov 9 at 0:24










  • @kunal_bohra. Yes, this is how I am using it in my 1st code, but available only in the callback, not in reduce (can't be used for initial value).
    – allez l'OM
    Nov 9 at 14:05














up vote
2
down vote

favorite












When chaining filter and reduce, how to get the size of the filtered array? I need that size for tailoring responsive design CSS.
My (simplified) code, uses the 3rd and 4th parameters of the callback:



json.articles
.filter(a => keep(a,name))
.reduce((el, a,i,t) =>
i===0? DOMtag('section','class':`media$t.length`)
.appendChild(DOMtag(`article`, content(a,0))).parentElement
: el.appendChild(DOMtag(`article`, content(a,i))).parentElement,
null);


or, even simpler (thanks to luc and his lazy evaluation suggestion below):



json.articles
.filter(a => keep(a,name))
.reduce((el, a,i,t) =>
(el || DOMtag('section','class':`media$t.length`))
.appendChild(DOMtag(`article`, content(a,i))).parentElement,
null);


Both code work, but if someone has an idea about binding this somehow, it would be possible to use the initial value of the accumulator, such as:



json.articles
.filter(a => keep(a,name))
.reduce((el, a) =>
el.appendChild(DOMtag(`article`, content(a,i))).parentElement,
DOMtag('section','class':`media$this.length`));


Any idea?










share|improve this question



















  • 1




    the 4th argument in the reducer function is the source array. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
    – kunal_bohra
    Nov 9 at 0:24










  • @kunal_bohra. Yes, this is how I am using it in my 1st code, but available only in the callback, not in reduce (can't be used for initial value).
    – allez l'OM
    Nov 9 at 14:05












up vote
2
down vote

favorite









up vote
2
down vote

favorite











When chaining filter and reduce, how to get the size of the filtered array? I need that size for tailoring responsive design CSS.
My (simplified) code, uses the 3rd and 4th parameters of the callback:



json.articles
.filter(a => keep(a,name))
.reduce((el, a,i,t) =>
i===0? DOMtag('section','class':`media$t.length`)
.appendChild(DOMtag(`article`, content(a,0))).parentElement
: el.appendChild(DOMtag(`article`, content(a,i))).parentElement,
null);


or, even simpler (thanks to luc and his lazy evaluation suggestion below):



json.articles
.filter(a => keep(a,name))
.reduce((el, a,i,t) =>
(el || DOMtag('section','class':`media$t.length`))
.appendChild(DOMtag(`article`, content(a,i))).parentElement,
null);


Both code work, but if someone has an idea about binding this somehow, it would be possible to use the initial value of the accumulator, such as:



json.articles
.filter(a => keep(a,name))
.reduce((el, a) =>
el.appendChild(DOMtag(`article`, content(a,i))).parentElement,
DOMtag('section','class':`media$this.length`));


Any idea?










share|improve this question















When chaining filter and reduce, how to get the size of the filtered array? I need that size for tailoring responsive design CSS.
My (simplified) code, uses the 3rd and 4th parameters of the callback:



json.articles
.filter(a => keep(a,name))
.reduce((el, a,i,t) =>
i===0? DOMtag('section','class':`media$t.length`)
.appendChild(DOMtag(`article`, content(a,0))).parentElement
: el.appendChild(DOMtag(`article`, content(a,i))).parentElement,
null);


or, even simpler (thanks to luc and his lazy evaluation suggestion below):



json.articles
.filter(a => keep(a,name))
.reduce((el, a,i,t) =>
(el || DOMtag('section','class':`media$t.length`))
.appendChild(DOMtag(`article`, content(a,i))).parentElement,
null);


Both code work, but if someone has an idea about binding this somehow, it would be possible to use the initial value of the accumulator, such as:



json.articles
.filter(a => keep(a,name))
.reduce((el, a) =>
el.appendChild(DOMtag(`article`, content(a,i))).parentElement,
DOMtag('section','class':`media$this.length`));


Any idea?







javascript arrays this method-chaining






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 19:33

























asked Nov 9 at 0:06









allez l'OM

10517




10517







  • 1




    the 4th argument in the reducer function is the source array. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
    – kunal_bohra
    Nov 9 at 0:24










  • @kunal_bohra. Yes, this is how I am using it in my 1st code, but available only in the callback, not in reduce (can't be used for initial value).
    – allez l'OM
    Nov 9 at 14:05












  • 1




    the 4th argument in the reducer function is the source array. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
    – kunal_bohra
    Nov 9 at 0:24










  • @kunal_bohra. Yes, this is how I am using it in my 1st code, but available only in the callback, not in reduce (can't be used for initial value).
    – allez l'OM
    Nov 9 at 14:05







1




1




the 4th argument in the reducer function is the source array. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– kunal_bohra
Nov 9 at 0:24




the 4th argument in the reducer function is the source array. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– kunal_bohra
Nov 9 at 0:24












@kunal_bohra. Yes, this is how I am using it in my 1st code, but available only in the callback, not in reduce (can't be used for initial value).
– allez l'OM
Nov 9 at 14:05




@kunal_bohra. Yes, this is how I am using it in my 1st code, but available only in the callback, not in reduce (can't be used for initial value).
– allez l'OM
Nov 9 at 14:05












1 Answer
1






active

oldest

votes

















up vote
0
down vote













There is no way to have access to the filtered array length at the moment of passing the default value of reduce since the expression is evaluated before reduce is called.



You can, though, simplify the contents of your function as follows:






// Sample data
const json = articles: [name: 'test',,name: 'test2',,];
// functional helper function to illustrate the example
function DOMTag(tagName, content, className)
const tag = document.createElement(tagName);

if (content)
tag.appendChild(document.createTextNode(content));

if (className)
tag.className = className;

return tag;


// Start of actual answer code:
const section = json.articles
.filter(a => a.name)
.map(a => DOMTag(`article`, content: a.name))
.reduce((p, c, i, a) =>
(p || DOMTag('section',className:`media$a.length`))
.appendChild(c).parentElement
, null);

section.className = `media$section.childNodes.length`

// Validation
console.log(section.outerHTML);





Another option that would be clean is to set the className outside of the chain:






// Sample data
const json = articles: [name: 'test',,name: 'test2',,];
// functional helper function to illustrate the example
function DOMTag(tagName, content)
const tag = document.createElement(tagName);
if (content)
tag.appendChild(document.createTextNode(content));

return tag;


// Start of actual answer code:
const section = json.articles
.filter(a => a.name)
.map(el => DOMTag('article', el.name))
.reduce((p,c) => (p.appendChild(c), p), DOMTag('section'))

section.className = `media$section.childNodes.length`

// Validation
console.log(section.outerHTML);





Option2: use a function to abstract the creation of the container:






// Sample data
const json = articles: [name: 'test',,name: 'test2',,];
// functional helper function to illustrate the example
function DOMTag(tagName, content, className)
const tag = document.createElement(tagName);
if (content)
tag.appendChild(document.createTextNode(content));

if (className)
tag.className = className;

return tag;


// Start of actual answer code:

function section(children)
const section = DOMTag('section',
className: `media$children.length`
);
children.forEach(c => section.appendChild(c));
return section;


const s = section(
json.articles
.filter(a => a.name)
.map(el => DOMTag('article', content: el.name))
)

// Validation
console.log(s.outerHTML);





Option 3: more functions! If you absolutely must use reduce:






// Sample data
const json = articles: [name: 'test',,name: 'test2',,];
// functional helper function to illustrate the example
function DOMTag(tagName, content, className)
const tag = document.createElement(tagName);
if (content)
tag.appendChild(document.createTextNode(content));

if (className)
tag.className = className;

return tag;


// Start of actual answer code:

function defaultSection()
let m = false;
return (a = ) =>
if (!m)
m = DOMTag('section', className: `media$a.length`);

return m;
;


const section = json.articles
.filter(a => a.name)
.map(el => DOMTag('article', content: el.name))
.reduce((p,c,i,a) =>
p(a).appendChild(c);
return p;
, defaultSection())();

// Validation
console.log(section.outerHTML);








share|improve this answer






















  • There is a way to pass the array to the callback (a synchronous callback), hence I believe that there should be a way to bind this to that array. Your workarounds #2 and #3 are intersting (#1 is not chainable), but not much better than my own first solution.
    – allez l'OM
    Nov 9 at 21:51










  • That's a fair point. I don't think you can use bind in the way you are thinking but you can take advantage of lazy evaluation of boolean expressions to simplify your initial code. Update the answer with a new option that might be interesting as well.
    – lucascaro
    Nov 10 at 0:58






  • 1




    I don't understand why that answer has been downvoted. At least the comment above is very useful.
    – allez l'OM
    Nov 10 at 19:28











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',
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
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53217989%2fjavascript-in-a-chain-how-to-get-size-of-previous-array%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













There is no way to have access to the filtered array length at the moment of passing the default value of reduce since the expression is evaluated before reduce is called.



You can, though, simplify the contents of your function as follows:






// Sample data
const json = articles: [name: 'test',,name: 'test2',,];
// functional helper function to illustrate the example
function DOMTag(tagName, content, className)
const tag = document.createElement(tagName);

if (content)
tag.appendChild(document.createTextNode(content));

if (className)
tag.className = className;

return tag;


// Start of actual answer code:
const section = json.articles
.filter(a => a.name)
.map(a => DOMTag(`article`, content: a.name))
.reduce((p, c, i, a) =>
(p || DOMTag('section',className:`media$a.length`))
.appendChild(c).parentElement
, null);

section.className = `media$section.childNodes.length`

// Validation
console.log(section.outerHTML);





Another option that would be clean is to set the className outside of the chain:






// Sample data
const json = articles: [name: 'test',,name: 'test2',,];
// functional helper function to illustrate the example
function DOMTag(tagName, content)
const tag = document.createElement(tagName);
if (content)
tag.appendChild(document.createTextNode(content));

return tag;


// Start of actual answer code:
const section = json.articles
.filter(a => a.name)
.map(el => DOMTag('article', el.name))
.reduce((p,c) => (p.appendChild(c), p), DOMTag('section'))

section.className = `media$section.childNodes.length`

// Validation
console.log(section.outerHTML);





Option2: use a function to abstract the creation of the container:






// Sample data
const json = articles: [name: 'test',,name: 'test2',,];
// functional helper function to illustrate the example
function DOMTag(tagName, content, className)
const tag = document.createElement(tagName);
if (content)
tag.appendChild(document.createTextNode(content));

if (className)
tag.className = className;

return tag;


// Start of actual answer code:

function section(children)
const section = DOMTag('section',
className: `media$children.length`
);
children.forEach(c => section.appendChild(c));
return section;


const s = section(
json.articles
.filter(a => a.name)
.map(el => DOMTag('article', content: el.name))
)

// Validation
console.log(s.outerHTML);





Option 3: more functions! If you absolutely must use reduce:






// Sample data
const json = articles: [name: 'test',,name: 'test2',,];
// functional helper function to illustrate the example
function DOMTag(tagName, content, className)
const tag = document.createElement(tagName);
if (content)
tag.appendChild(document.createTextNode(content));

if (className)
tag.className = className;

return tag;


// Start of actual answer code:

function defaultSection()
let m = false;
return (a = ) =>
if (!m)
m = DOMTag('section', className: `media$a.length`);

return m;
;


const section = json.articles
.filter(a => a.name)
.map(el => DOMTag('article', content: el.name))
.reduce((p,c,i,a) =>
p(a).appendChild(c);
return p;
, defaultSection())();

// Validation
console.log(section.outerHTML);








share|improve this answer






















  • There is a way to pass the array to the callback (a synchronous callback), hence I believe that there should be a way to bind this to that array. Your workarounds #2 and #3 are intersting (#1 is not chainable), but not much better than my own first solution.
    – allez l'OM
    Nov 9 at 21:51










  • That's a fair point. I don't think you can use bind in the way you are thinking but you can take advantage of lazy evaluation of boolean expressions to simplify your initial code. Update the answer with a new option that might be interesting as well.
    – lucascaro
    Nov 10 at 0:58






  • 1




    I don't understand why that answer has been downvoted. At least the comment above is very useful.
    – allez l'OM
    Nov 10 at 19:28















up vote
0
down vote













There is no way to have access to the filtered array length at the moment of passing the default value of reduce since the expression is evaluated before reduce is called.



You can, though, simplify the contents of your function as follows:






// Sample data
const json = articles: [name: 'test',,name: 'test2',,];
// functional helper function to illustrate the example
function DOMTag(tagName, content, className)
const tag = document.createElement(tagName);

if (content)
tag.appendChild(document.createTextNode(content));

if (className)
tag.className = className;

return tag;


// Start of actual answer code:
const section = json.articles
.filter(a => a.name)
.map(a => DOMTag(`article`, content: a.name))
.reduce((p, c, i, a) =>
(p || DOMTag('section',className:`media$a.length`))
.appendChild(c).parentElement
, null);

section.className = `media$section.childNodes.length`

// Validation
console.log(section.outerHTML);





Another option that would be clean is to set the className outside of the chain:






// Sample data
const json = articles: [name: 'test',,name: 'test2',,];
// functional helper function to illustrate the example
function DOMTag(tagName, content)
const tag = document.createElement(tagName);
if (content)
tag.appendChild(document.createTextNode(content));

return tag;


// Start of actual answer code:
const section = json.articles
.filter(a => a.name)
.map(el => DOMTag('article', el.name))
.reduce((p,c) => (p.appendChild(c), p), DOMTag('section'))

section.className = `media$section.childNodes.length`

// Validation
console.log(section.outerHTML);





Option2: use a function to abstract the creation of the container:






// Sample data
const json = articles: [name: 'test',,name: 'test2',,];
// functional helper function to illustrate the example
function DOMTag(tagName, content, className)
const tag = document.createElement(tagName);
if (content)
tag.appendChild(document.createTextNode(content));

if (className)
tag.className = className;

return tag;


// Start of actual answer code:

function section(children)
const section = DOMTag('section',
className: `media$children.length`
);
children.forEach(c => section.appendChild(c));
return section;


const s = section(
json.articles
.filter(a => a.name)
.map(el => DOMTag('article', content: el.name))
)

// Validation
console.log(s.outerHTML);





Option 3: more functions! If you absolutely must use reduce:






// Sample data
const json = articles: [name: 'test',,name: 'test2',,];
// functional helper function to illustrate the example
function DOMTag(tagName, content, className)
const tag = document.createElement(tagName);
if (content)
tag.appendChild(document.createTextNode(content));

if (className)
tag.className = className;

return tag;


// Start of actual answer code:

function defaultSection()
let m = false;
return (a = ) =>
if (!m)
m = DOMTag('section', className: `media$a.length`);

return m;
;


const section = json.articles
.filter(a => a.name)
.map(el => DOMTag('article', content: el.name))
.reduce((p,c,i,a) =>
p(a).appendChild(c);
return p;
, defaultSection())();

// Validation
console.log(section.outerHTML);








share|improve this answer






















  • There is a way to pass the array to the callback (a synchronous callback), hence I believe that there should be a way to bind this to that array. Your workarounds #2 and #3 are intersting (#1 is not chainable), but not much better than my own first solution.
    – allez l'OM
    Nov 9 at 21:51










  • That's a fair point. I don't think you can use bind in the way you are thinking but you can take advantage of lazy evaluation of boolean expressions to simplify your initial code. Update the answer with a new option that might be interesting as well.
    – lucascaro
    Nov 10 at 0:58






  • 1




    I don't understand why that answer has been downvoted. At least the comment above is very useful.
    – allez l'OM
    Nov 10 at 19:28













up vote
0
down vote










up vote
0
down vote









There is no way to have access to the filtered array length at the moment of passing the default value of reduce since the expression is evaluated before reduce is called.



You can, though, simplify the contents of your function as follows:






// Sample data
const json = articles: [name: 'test',,name: 'test2',,];
// functional helper function to illustrate the example
function DOMTag(tagName, content, className)
const tag = document.createElement(tagName);

if (content)
tag.appendChild(document.createTextNode(content));

if (className)
tag.className = className;

return tag;


// Start of actual answer code:
const section = json.articles
.filter(a => a.name)
.map(a => DOMTag(`article`, content: a.name))
.reduce((p, c, i, a) =>
(p || DOMTag('section',className:`media$a.length`))
.appendChild(c).parentElement
, null);

section.className = `media$section.childNodes.length`

// Validation
console.log(section.outerHTML);





Another option that would be clean is to set the className outside of the chain:






// Sample data
const json = articles: [name: 'test',,name: 'test2',,];
// functional helper function to illustrate the example
function DOMTag(tagName, content)
const tag = document.createElement(tagName);
if (content)
tag.appendChild(document.createTextNode(content));

return tag;


// Start of actual answer code:
const section = json.articles
.filter(a => a.name)
.map(el => DOMTag('article', el.name))
.reduce((p,c) => (p.appendChild(c), p), DOMTag('section'))

section.className = `media$section.childNodes.length`

// Validation
console.log(section.outerHTML);





Option2: use a function to abstract the creation of the container:






// Sample data
const json = articles: [name: 'test',,name: 'test2',,];
// functional helper function to illustrate the example
function DOMTag(tagName, content, className)
const tag = document.createElement(tagName);
if (content)
tag.appendChild(document.createTextNode(content));

if (className)
tag.className = className;

return tag;


// Start of actual answer code:

function section(children)
const section = DOMTag('section',
className: `media$children.length`
);
children.forEach(c => section.appendChild(c));
return section;


const s = section(
json.articles
.filter(a => a.name)
.map(el => DOMTag('article', content: el.name))
)

// Validation
console.log(s.outerHTML);





Option 3: more functions! If you absolutely must use reduce:






// Sample data
const json = articles: [name: 'test',,name: 'test2',,];
// functional helper function to illustrate the example
function DOMTag(tagName, content, className)
const tag = document.createElement(tagName);
if (content)
tag.appendChild(document.createTextNode(content));

if (className)
tag.className = className;

return tag;


// Start of actual answer code:

function defaultSection()
let m = false;
return (a = ) =>
if (!m)
m = DOMTag('section', className: `media$a.length`);

return m;
;


const section = json.articles
.filter(a => a.name)
.map(el => DOMTag('article', content: el.name))
.reduce((p,c,i,a) =>
p(a).appendChild(c);
return p;
, defaultSection())();

// Validation
console.log(section.outerHTML);








share|improve this answer














There is no way to have access to the filtered array length at the moment of passing the default value of reduce since the expression is evaluated before reduce is called.



You can, though, simplify the contents of your function as follows:






// Sample data
const json = articles: [name: 'test',,name: 'test2',,];
// functional helper function to illustrate the example
function DOMTag(tagName, content, className)
const tag = document.createElement(tagName);

if (content)
tag.appendChild(document.createTextNode(content));

if (className)
tag.className = className;

return tag;


// Start of actual answer code:
const section = json.articles
.filter(a => a.name)
.map(a => DOMTag(`article`, content: a.name))
.reduce((p, c, i, a) =>
(p || DOMTag('section',className:`media$a.length`))
.appendChild(c).parentElement
, null);

section.className = `media$section.childNodes.length`

// Validation
console.log(section.outerHTML);





Another option that would be clean is to set the className outside of the chain:






// Sample data
const json = articles: [name: 'test',,name: 'test2',,];
// functional helper function to illustrate the example
function DOMTag(tagName, content)
const tag = document.createElement(tagName);
if (content)
tag.appendChild(document.createTextNode(content));

return tag;


// Start of actual answer code:
const section = json.articles
.filter(a => a.name)
.map(el => DOMTag('article', el.name))
.reduce((p,c) => (p.appendChild(c), p), DOMTag('section'))

section.className = `media$section.childNodes.length`

// Validation
console.log(section.outerHTML);





Option2: use a function to abstract the creation of the container:






// Sample data
const json = articles: [name: 'test',,name: 'test2',,];
// functional helper function to illustrate the example
function DOMTag(tagName, content, className)
const tag = document.createElement(tagName);
if (content)
tag.appendChild(document.createTextNode(content));

if (className)
tag.className = className;

return tag;


// Start of actual answer code:

function section(children)
const section = DOMTag('section',
className: `media$children.length`
);
children.forEach(c => section.appendChild(c));
return section;


const s = section(
json.articles
.filter(a => a.name)
.map(el => DOMTag('article', content: el.name))
)

// Validation
console.log(s.outerHTML);





Option 3: more functions! If you absolutely must use reduce:






// Sample data
const json = articles: [name: 'test',,name: 'test2',,];
// functional helper function to illustrate the example
function DOMTag(tagName, content, className)
const tag = document.createElement(tagName);
if (content)
tag.appendChild(document.createTextNode(content));

if (className)
tag.className = className;

return tag;


// Start of actual answer code:

function defaultSection()
let m = false;
return (a = ) =>
if (!m)
m = DOMTag('section', className: `media$a.length`);

return m;
;


const section = json.articles
.filter(a => a.name)
.map(el => DOMTag('article', content: el.name))
.reduce((p,c,i,a) =>
p(a).appendChild(c);
return p;
, defaultSection())();

// Validation
console.log(section.outerHTML);








// Sample data
const json = articles: [name: 'test',,name: 'test2',,];
// functional helper function to illustrate the example
function DOMTag(tagName, content, className)
const tag = document.createElement(tagName);

if (content)
tag.appendChild(document.createTextNode(content));

if (className)
tag.className = className;

return tag;


// Start of actual answer code:
const section = json.articles
.filter(a => a.name)
.map(a => DOMTag(`article`, content: a.name))
.reduce((p, c, i, a) =>
(p || DOMTag('section',className:`media$a.length`))
.appendChild(c).parentElement
, null);

section.className = `media$section.childNodes.length`

// Validation
console.log(section.outerHTML);





// Sample data
const json = articles: [name: 'test',,name: 'test2',,];
// functional helper function to illustrate the example
function DOMTag(tagName, content, className)
const tag = document.createElement(tagName);

if (content)
tag.appendChild(document.createTextNode(content));

if (className)
tag.className = className;

return tag;


// Start of actual answer code:
const section = json.articles
.filter(a => a.name)
.map(a => DOMTag(`article`, content: a.name))
.reduce((p, c, i, a) =>
(p || DOMTag('section',className:`media$a.length`))
.appendChild(c).parentElement
, null);

section.className = `media$section.childNodes.length`

// Validation
console.log(section.outerHTML);





// Sample data
const json = articles: [name: 'test',,name: 'test2',,];
// functional helper function to illustrate the example
function DOMTag(tagName, content)
const tag = document.createElement(tagName);
if (content)
tag.appendChild(document.createTextNode(content));

return tag;


// Start of actual answer code:
const section = json.articles
.filter(a => a.name)
.map(el => DOMTag('article', el.name))
.reduce((p,c) => (p.appendChild(c), p), DOMTag('section'))

section.className = `media$section.childNodes.length`

// Validation
console.log(section.outerHTML);





// Sample data
const json = articles: [name: 'test',,name: 'test2',,];
// functional helper function to illustrate the example
function DOMTag(tagName, content)
const tag = document.createElement(tagName);
if (content)
tag.appendChild(document.createTextNode(content));

return tag;


// Start of actual answer code:
const section = json.articles
.filter(a => a.name)
.map(el => DOMTag('article', el.name))
.reduce((p,c) => (p.appendChild(c), p), DOMTag('section'))

section.className = `media$section.childNodes.length`

// Validation
console.log(section.outerHTML);





// Sample data
const json = articles: [name: 'test',,name: 'test2',,];
// functional helper function to illustrate the example
function DOMTag(tagName, content, className)
const tag = document.createElement(tagName);
if (content)
tag.appendChild(document.createTextNode(content));

if (className)
tag.className = className;

return tag;


// Start of actual answer code:

function section(children)
const section = DOMTag('section',
className: `media$children.length`
);
children.forEach(c => section.appendChild(c));
return section;


const s = section(
json.articles
.filter(a => a.name)
.map(el => DOMTag('article', content: el.name))
)

// Validation
console.log(s.outerHTML);





// Sample data
const json = articles: [name: 'test',,name: 'test2',,];
// functional helper function to illustrate the example
function DOMTag(tagName, content, className)
const tag = document.createElement(tagName);
if (content)
tag.appendChild(document.createTextNode(content));

if (className)
tag.className = className;

return tag;


// Start of actual answer code:

function section(children)
const section = DOMTag('section',
className: `media$children.length`
);
children.forEach(c => section.appendChild(c));
return section;


const s = section(
json.articles
.filter(a => a.name)
.map(el => DOMTag('article', content: el.name))
)

// Validation
console.log(s.outerHTML);





// Sample data
const json = articles: [name: 'test',,name: 'test2',,];
// functional helper function to illustrate the example
function DOMTag(tagName, content, className)
const tag = document.createElement(tagName);
if (content)
tag.appendChild(document.createTextNode(content));

if (className)
tag.className = className;

return tag;


// Start of actual answer code:

function defaultSection()
let m = false;
return (a = ) =>
if (!m)
m = DOMTag('section', className: `media$a.length`);

return m;
;


const section = json.articles
.filter(a => a.name)
.map(el => DOMTag('article', content: el.name))
.reduce((p,c,i,a) =>
p(a).appendChild(c);
return p;
, defaultSection())();

// Validation
console.log(section.outerHTML);





// Sample data
const json = articles: [name: 'test',,name: 'test2',,];
// functional helper function to illustrate the example
function DOMTag(tagName, content, className)
const tag = document.createElement(tagName);
if (content)
tag.appendChild(document.createTextNode(content));

if (className)
tag.className = className;

return tag;


// Start of actual answer code:

function defaultSection()
let m = false;
return (a = ) =>
if (!m)
m = DOMTag('section', className: `media$a.length`);

return m;
;


const section = json.articles
.filter(a => a.name)
.map(el => DOMTag('article', content: el.name))
.reduce((p,c,i,a) =>
p(a).appendChild(c);
return p;
, defaultSection())();

// Validation
console.log(section.outerHTML);






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 10 at 0:56

























answered Nov 9 at 1:28









lucascaro

3,34611530




3,34611530











  • There is a way to pass the array to the callback (a synchronous callback), hence I believe that there should be a way to bind this to that array. Your workarounds #2 and #3 are intersting (#1 is not chainable), but not much better than my own first solution.
    – allez l'OM
    Nov 9 at 21:51










  • That's a fair point. I don't think you can use bind in the way you are thinking but you can take advantage of lazy evaluation of boolean expressions to simplify your initial code. Update the answer with a new option that might be interesting as well.
    – lucascaro
    Nov 10 at 0:58






  • 1




    I don't understand why that answer has been downvoted. At least the comment above is very useful.
    – allez l'OM
    Nov 10 at 19:28

















  • There is a way to pass the array to the callback (a synchronous callback), hence I believe that there should be a way to bind this to that array. Your workarounds #2 and #3 are intersting (#1 is not chainable), but not much better than my own first solution.
    – allez l'OM
    Nov 9 at 21:51










  • That's a fair point. I don't think you can use bind in the way you are thinking but you can take advantage of lazy evaluation of boolean expressions to simplify your initial code. Update the answer with a new option that might be interesting as well.
    – lucascaro
    Nov 10 at 0:58






  • 1




    I don't understand why that answer has been downvoted. At least the comment above is very useful.
    – allez l'OM
    Nov 10 at 19:28
















There is a way to pass the array to the callback (a synchronous callback), hence I believe that there should be a way to bind this to that array. Your workarounds #2 and #3 are intersting (#1 is not chainable), but not much better than my own first solution.
– allez l'OM
Nov 9 at 21:51




There is a way to pass the array to the callback (a synchronous callback), hence I believe that there should be a way to bind this to that array. Your workarounds #2 and #3 are intersting (#1 is not chainable), but not much better than my own first solution.
– allez l'OM
Nov 9 at 21:51












That's a fair point. I don't think you can use bind in the way you are thinking but you can take advantage of lazy evaluation of boolean expressions to simplify your initial code. Update the answer with a new option that might be interesting as well.
– lucascaro
Nov 10 at 0:58




That's a fair point. I don't think you can use bind in the way you are thinking but you can take advantage of lazy evaluation of boolean expressions to simplify your initial code. Update the answer with a new option that might be interesting as well.
– lucascaro
Nov 10 at 0:58




1




1




I don't understand why that answer has been downvoted. At least the comment above is very useful.
– allez l'OM
Nov 10 at 19:28





I don't understand why that answer has been downvoted. At least the comment above is very useful.
– allez l'OM
Nov 10 at 19:28


















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53217989%2fjavascript-in-a-chain-how-to-get-size-of-previous-array%23new-answer', 'question_page');

);

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







Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)