Is possible to rewrite all values in array without for loop in javascript? -
let's suppose have array (myarray) data this:
0: myarray content: 'something' date: '15.5.2015' name: 'abc' 1: myarray content: 'text' date: '15.5.2015' name: 'bla' 2: etc ...
now rewriting values (into e.g. empty string) of 1 object properties in array (for example: 'content') use loop this:
for(var = 0; < myarray.length; i++){ myarray[i].content = ''; }
so result of be:
0: myarray content: '' date: '15.5.2015' name: 'abc' 1: myarray content: '' date: '15.5.2015' name: 'bla' 2: etc ...
my question is: possible same result without using loop in javascript? this:
myarray[all].content.rewriteinto('');
tnx ideas.
anything end looping in fashion. however, you don't have loop...
because have functional array methods! you're looking map
or reduce
, perhaps both, since want transform (map) each element and/or combine them 1 (reduce).
as example, can take data , return string of content
fields concatenated using:
var data = [{ content: 'something', date: '15.5.2015', name: 'abc' }, { content: 'text', date: '15.5.2015', name: 'bla' }]; var result = data.map(function(it) { return it.content; }).join(' '); document.getelementbyid('r').textcontent = json.stringify(result);
<pre id="r"></pre>
to remove content
field each item, without modifying input, can:
var data = [{ content: 'something', date: '15.5.2015', name: 'abc' }, { content: 'text', date: '15.5.2015', name: 'bla' }]; var result = data.map(function(it) { return {date: it.date, name: it.name}; }); document.getelementbyid('r').textcontent = json.stringify(result);
<pre id="r"></pre>
Comments
Post a Comment