[英]Is possible to rewrite all values in array without for loop in javascript?
Let's suppose that we have an array (myArray) with data like this: 假设我们有一个数组(myArray),其数据如下:
0: myArray
content: 'something'
date: '15.5.2015'
name: 'Abc'
1: myArray
content: 'text'
date: '15.5.2015'
name: 'Bla'
2: etc ...
Now for rewriting all values (into eg empty string) of one object properties in this array (for example: 'content') I would use for loop like this: 现在,为了重写该数组中一个对象属性的所有值(例如,变成空字符串)(例如:“ content”),我将使用如下的for循环:
for(var i = 0; i < myArray.length; i++){
myArray[i].content = '';
}
So result of this would be: 因此,结果将是:
0: myArray
content: ''
date: '15.5.2015'
name: 'Abc'
1: myArray
content: ''
date: '15.5.2015'
name: 'Bla'
2: etc ...
My question is: Is possible to do same result without using loop in javascript? 我的问题是:是否可以在不使用javascript循环的情况下做同样的结果? Something like this:
像这样:
myArray[all].content.RewriteInto('');
Tnx for ideas. Tnx的想法。
Anything you do will end up looping in some fashion. 您所做的任何事情最终都会以某种方式循环播放。 However, you don't have to loop...
但是, 您不必循环...
because we now have functional array methods! 因为我们现在有了函数数组方法! You're probably looking for
map
or reduce
, perhaps both, since you want to transform (map) each element and/or combine them into one (reduce). 您可能正在寻找
map
或reduce
,也许两者都有,因为您想要转换(映射)每个元素和/或将它们组合为一个(reduce)。
As an example, we can take your data and return a string with all of the content
fields concatenated using: 例如,我们可以获取您的数据并返回一个字符串,其中所有
content
字段都使用以下方式连接:
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 the content
field from each item, without modifying the input, you can: 要从每个项目中删除
content
字段,而无需修改输入,您可以:
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>
You should look at the map function . 您应该查看map函数 。 Use it like this :
像这样使用它:
myArray.map(function (data) {
return data.content = 'value';
}
As the first comment on your question points out you always have to use a loop, but you could monkey patch the Array using prototype like: 正如您对问题的第一条评论指出的那样,您始终必须使用循环,但是您可以使用以下原型对数组进行猴子修补:
Array.prototype.rewriteInto = function(key, rewriteVal){
for(var k in this){
this[k][key] = rewriteVal;
}
};
var testData = new Array();
testData.push({
content: 'something',
date: '15.5.2015',
name: 'Abc',
});
testData.push({
content: 'something',
date: '15.5.2015',
name: 'bla',
});
console.log(testData);
testData.rewriteInto('content', '');
console.log(testData);
So you don't have to rewrite the loop all the time you want to use this functionality. 因此,您不必每次都想使用此功能时重写循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.