我正在尝试将此函数转换为使用reduce。我中途了。当所选值为true
时,我想知道该元素的索引。
let options = [
{label: 'foo1', selected: false},
{label: 'foo2', selected: true},
{label: 'foo2', selected: false},
{label: 'foo2', selected: false},
{label: 'foo2', selected: false},
];
const obj = options
.map((option, index) => (option.selected ? index : -1))
.filter((val) => val !== -1)[0];
结果:1
我的尝试是这样:
const obj = options.reduce((acc, currentValue, index) => {
const i = currentValue["selected"] ? index : -1;
return acc.concat(i)
}, []); // [-1,1,-1,-1,-1]
如何更改整个要使用reduce的内容?
答案 0 :(得分:1)
仅当option.selected
为true时,才将索引添加到结果中,否则不执行任何操作。
let options = [
{label: 'foo1', selected: false},
{label: 'foo2', selected: true},
{label: 'foo2', selected: false},
{label: 'foo2', selected: false},
{label: 'foo2', selected: false},
];
const selected = options.reduce((arr, option, index) => {
return option.selected ? arr.concat(index) : arr;
}, []);
console.log(selected);
答案 1 :(得分:1)
在concat()
中使用三进制来设置要插入的值。 Array#concat()
返回更新后的数组,因此返回reduce()
。
或使用传播返回新数组[...accumulator, selsected ? i :-1]
const res = options.reduce((a, {selected:s}, i) => a.concat(s ? i : -1) , []);
// OR
// const res = options.reduce((a, {selected:s}, i) => [....a,s ? i : -1] , []);
console.log(res)
<script>
let options = [
{label: 'foo1', selected: false},
{label: 'foo2', selected: true},
{label: 'foo2', selected: false},
{label: 'foo2', selected: false},
{label: 'foo2', selected: false},
];
</script>
答案 2 :(得分:0)
如果您希望使用selected
= true的索引,则可以使用reduce
执行类似的操作。如果上下文中的当前项具有selected
=== true,则将索引添加到累加器,否则返回累加器。当前,如果条件为-1
false
let options = [
{label: 'foo1', selected: false},
{label: 'foo2', selected: true},
{label: 'foo2', selected: false},
{label: 'foo2', selected: false},
{label: 'foo2', selected: false},
];
const selectedIndexes = options.reduce((r, o, i) => o.selected ? r.concat(i) : r, [])
console.log(selectedIndexes)
如果您需要整个对象,则只需使用filter
:
let options = [{label:'foo1',selected:false},{label:'foo2',selected:true},{label:'foo2',selected:false},{label:'foo2',selected:false},{label:'foo2',selected:false},];
const filtered = options.filter(o => o.selected)
console.log(filtered)
答案 3 :(得分:0)
如果class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {
private lateinit var navController: NavController
private lateinit var appBarConfiguration: AppBarConfiguration
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
navController = findNavController(R.id.nav_controller_fragment)
appBarConfiguration = AppBarConfiguration(navController.graph,drawer_layout)
setupActionBarWithNavController(navController,appBarConfiguration)
drawer()
setupNavigationMenu()
nav_view.setNavigationItemSelectedListener(this)
}
private fun drawer() {
drawer_layout.addDrawerListener(object :DrawerLayout.DrawerListener{
override fun onDrawerStateChanged(newState: Int) {
}
override fun onDrawerSlide(drawerView: View, slideOffset: Float) {
}
override fun onDrawerClosed(drawerView: View) {
}
override fun onDrawerOpened(drawerView: View) {
}
})
}
private fun setupNavigationMenu() {
nav_view.let {
NavigationUI.setupWithNavController(it, navController)
}
}
override fun onBackPressed() {
if (drawer_layout.isDrawerOpen(GravityCompat.START)) {
drawer_layout.closeDrawer(GravityCompat.START)
} else {
super.onBackPressed()
}
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.main, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.action_settings -> true
else -> NavigationUI.onNavDestinationSelected(item,navController) || super.onOptionsItemSelected(item)
}
}
override fun onNavigationItemSelected(item: MenuItem): Boolean {
drawer_layout.closeDrawer(GravityCompat.START)
return when (item.itemId) {
R.id.nav_gallery -> true
else ->
NavigationUI.onNavDestinationSelected(
item,navController) || super.onOptionsItemSelected(item)
}
}
override fun onSupportNavigateUp(): Boolean {
return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
}
为...
,则使用Array#reduce
和selected
扩展运算符保留现有索引并添加新索引。
true
答案 4 :(得分:0)
另一种方法是使用push
代替concat
:
let options = [
{label: 'foo1', selected: false},
{label: 'foo2', selected: true},
{label: 'foo2', selected: false},
{label: 'foo2', selected: false},
{label: 'foo2', selected: false},
];
var obj = options.reduce( (acc, currentValue, index) => {
currentValue.selected ? acc.push(index) : ''; return acc} ,[] ) ;
console.log(obj)