简体   繁体   English

从对象列表更新基于另一个列表下划线的属性

[英]From a list of objects update a property based on another list underscorejs

I have a list of objects 'objlist' and a list of ids 'idlist'. 我有一个对象'objlist'的列表和一个ID'idlist'的列表。

var objlist = [{id:1, visible:true},{id:2, visible:false},{id:3, visible:false}];
var idlist = [1, 3];

I want the objects in the first list to have value of 'visible' as 'true' for every ids in second list, and the remaining to have value 'false'. 我希望第二个列表中的每个ID的第一个列表中的对象的值都为“可见”,为“ true”,其余的对象的值均为“ false”。

When I try with loop within a loop, I get incorrect result. 当我在一个循环中尝试使用循环时,得到的结果不正确。 How to get the proper list. 如何获得正确的清单。

My attempt: 我的尝试:

 var objlist = [{id:1, visible:true},{id:2, visible:false},{id:3, visible:false}]; var idlist = [1, 3]; _.each(idlist, function(p) { _.each(objlist, function(obj) { if (obj.id == p) { obj.visible = true; } else { obj.visible = false; } }); }); console.log(objlist) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script> 

What happens is when the condition breaks in subsequent loops, the values get changed. 当条件在后续循环中中断时,发生的情况是值被更改。

To start with, why you code does not work: 首先,为什么您的代码无法正常工作:

You are looping on idList first and then looping on objlist . 您首先在idList上循环,然后在objlist循环。 So the issue here is, on every iteration, only 1 object can have true for condition. 因此,这里的问题是,在每次迭代中,只有1个对象可以满足条件。 Hence the last one is true . 因此,最后一个是true

Following is a sample using underscore: 以下是使用下划线的示例:

Note: Other answers have already shown approaches using vanilla JS, but since you already are using underscore, you can check this approach 注意:其他答案已经显示了使用Vanilla JS的方法,但是由于您已经在使用下划线,因此可以检查此方法

 var objlist = [{id:1, visible:true},{id:2, visible:false},{id:3, visible:false}]; var idlist = [1, 3]; _.each(objlist, function(p) { p.visible = _.contains(idlist, p.id) }); console.log(objlist) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script> 

Use forEach and includes 用于forEachincludes

objlist.forEach( s => (s.visible = idlist.includes( s.id ) ) );

Demo 演示

 var objlist = [{id:1, visible:true},{id:2, visible:false},{id:3, visible:false}]; var idlist = [1, 3]; objlist.forEach( s => (s.visible = idlist.includes( s.id ) ) ); console.log(objlist); 

You can use array.prototype.map and array.prototype.includes : 您可以使用array.prototype.maparray.prototype.includes

 var objlist = [{id:1, visible:true},{id:2, visible:false},{id:3, visible:false}]; var idlist = [1, 3]; var result = objlist.map(e => (e.visible = idlist.includes(e.id), e)); console.log(result); 

Try following 尝试跟随

 var objlist = [{id:1, visible:true},{id:2, visible:false},{id:3, visible:false}]; var idlist = [1, 3]; objlist = objlist.map(function(item){ item.visible = idlist.indexOf(item.id) !== -1; return item; }); console.log(objlist); 

This works. 这可行。

 var objlist = [{id:1, visible:true},{id:2, visible:false},{id:3, visible:false}]; var idlist = [1, 3]; for(var index in objlist) { var obj = objlist[index]; obj.visible = idlist.includes(obj.id); } console.log(objlist); 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM