简体   繁体   English

是否可以重写数组中的所有值而无需在JavaScript中进行for循环?

[英]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). 您可能正在寻找mapreduce ,也许两者都有,因为您想要转换(映射)每个元素和/或将它们组合为一个(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. 因此,您不必每次都想使用此功能时重写循环。

See example 例子

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法评估Java for循环中数组的所有值 - Trouble evaluating all values of an array in a for loop in Javascript 将javascript数组中的所有值初始化为0,无需迭代 - initialize all the values in a javascript array to 0 without iteration Javascript在没有for循环的Array of Objects值中找到Array元素 - Javascript find Array elements within Array of Objects values without a for loop JavaScript:获取二维数组中某个字段的所有可能值 - JavaScript: get all possible values of a field in a two-dimensional array javascript 中没有 for 循环的数组中的所有类 select 没有更简单的方法吗? - Is there not a more simple way to select all classes in an array without a for loop in javascript? Javascript循环,用于在没有eval的情况下从数组动态变量(和值) - Javascript loop for dynamic variables (and values) from array without eval 如何在 JavaScript 中计算没有 for 循环的对象数组中的值差异? - How to calculate values differences in an array of objects without a for loop, in JavaScript? JavaScript,使用 for 循环计算数组中所有值的总和 - JavaScript, Using a for loop to calculate the sum of all the values within an array Javascript数组在不知道父键的情况下获取所有子值 - Javascript array get all child values without knowing the parent key 递归遍历对象数组以使用Javascript构建所有可能路由的数组 - Recursively loop through array of objects to build an array of all possible routes with Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM