简体   繁体   English

如何向对象添加数组,但是为每个数组元素过滤除了一列以外的所有数组?

[英]How can I add an array to an object but filter out all but one column for each array element?

I have an array on my page called answers that looks like this: 我的页面上有一个名为answers的数组,如下所示:

answers[{x: 1, r: true;},{x: 2,r: false;},{x: 3, r: true;}]

Hopefully I wrote this correctly. 希望我写得正确。 There are a variable number of rows in the answers array (this time it's three but it could be anything under ten). 答案数组中有一个可变数量的行(这次是三个,但可能是十个以下的任何行)。 Each row contains an x field and a r field. 每行包含一个x字段和一个r字段。

Can someone tell me how I can add the answers array (or recreate another similar one) but ONLY the r field of each row of the answers array to the following object: 有人可以告诉我如何添加答案数组(或重新创建另一个类似的数组),但只​​有答案数组的每一行的r字段为以下对象:

$scope.so.xHeaders[fromParams]

你可以在Underscore.js中使用pluck方法:

var newList = _.pluck(answers, 'r');

You can do something like that: 你可以这样做:

$scope.so.xHeaders = []
angular.forEach(answers, function(value, index) {
    $scope.so.xHeaders.push({ r: value.r });
});

Depending on what you need there are a couple of ways of doing this. 根据您的需要,有几种方法可以做到这一点。

var answers = [{x: 1, r: true},{x: 2, r: false},{x: 3, r: true}]

1) If you just want an array of values: 1)如果您只想要一组值:

var arr = answers.map(function(el){
  return el.r;
}, []);

console.log(arr) // [true, false, true]

2) If you want to return the objects but only containing the r value: 2)如果要返回对象但仅包含r值:

var arr = answers.map(function(el){
  var obj = {};
  obj.r = el.r;
  return obj;
}, []);

(or shorter): (或更短):

var arr = answers.map(function(el){
  return { r: el.r };
}, []);

console.log(arr) // [Object { r=true}, Object { r=false}, Object { r=true}]

Fiddle . 小提琴

暂无
暂无

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

相关问题 如何在javascript中过滤和修改数组中的每个对象? - How can i filter and modify each object in array in javascript? 如何使用javascript或angular将一个数组的每个元素添加到其他数组中的对应元素 - How can I add each element of one array to corresponding elements in other arrays with javascript or angular 如何从网页获取JSON对象数组并将每个元素添加到HTML中的行? - How can i get an array of JSON object from a webpage and add each element to the row in HTML? 如何遍历数组对象并检查每个元素以查看是否已定义? - How can I loop through an array object and check each element to see if one is defined? 如何将fourier.getEnergy() 的每个元素添加到数组中? - How can I add each element of fourier.getEnergy() into an array? 如何从多个 arrays 中获取每个 object 并将它们全部放入一个数组中 - How can I get each object from multiple arrays and put them all into one array 如何为数组中的每个对象添加唯一 ID? - How can I add a unique ID to each object in an array? 如何将数组 1 的总和添加到数组 2 中的每个元素 - How do I add the sum of array 1 to each element in array 2 在将对象数组转换为 xml 时,如何分别摆脱由数组键包裹的每个元素? - How can I get rid of each element wrapped by the array key separately while converting array of object to xml? 如何处理此JSON数组,以删除数组的每个元素中的包装器对象? 使用JavaScript - How can I process this JSON array to remove a wrapper object in each element of the array? using JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM