Spa*_*000 0 javascript reactjs
在React上下文中,我试图渲染一些具有如下结构的对象:
const regions = [
{
displayName: 'NYC',
locations: [
{name: 'thing', isVisible: false},
{name: 'thing1', isVisible: false},
{name: 'thing3', isVisible: false},
],
},
{
displayName: 'Seattle',
locations: [
{name: 'thing', isVisible: true},
{name: 'thing1', isVisible: true},
{name: 'thing3', isVisible: true},
],
....
},
....
];
Run Code Online (Sandbox Code Playgroud)
我的问题是第一个对象与所有对象的上下文,isVisible: false如果所有isVisible值都为假,是否可以删除整个对象?基本上使用这个例子,这个对象:
{
displayName: 'NYC',
locations: [
{name: 'thing', isVisible: false},
{name: 'thing1', isVisible: false},
{name: 'thing3', isVisible: false},
],
},
Run Code Online (Sandbox Code Playgroud)
将被删除.
如果是这样,怎么样?如果没有,这是否可以实现?有没有其他解决方案?
使用数组过滤很容易
// filter only returns regions where the internal function evaluates to true
regions.filter(function(region){
// some returns true if at least one of the elements in the array causes the function below to return true
return region.locations.some(function(location){
return location.isVisible
})
})
Run Code Online (Sandbox Code Playgroud)
问题是你无法重新分配它,regions因为它是一个const
| 归档时间: |
|
| 查看次数: |
83 次 |
| 最近记录: |