[英]How to convert this array of objects to use reduce javascript
I'm trying to convert this function to use reduce. 我正在尝试将此函数转换为使用reduce。 I am halfway through.
我中途了。 When the selected value is
true
I want to know the index of that element. 当选定的值为
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];
Result: 1
结果:
1
My attempt is this: 我的尝试是这样的:
const obj = options.reduce((acc, currentValue, index) => {
const i = currentValue["selected"] ? index : -1;
return acc.concat(i)
}, []); // [-1,1,-1,-1,-1]
How do I change the entire thing to use reduce? 我如何改变整个东西使用减少?
Add the index to the result only if option.selected
is true otherwise do nothing. 仅当
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);
Use a ternary in concat()
to set the value to be inserted. 在
concat()
使用三元数来设置要插入的值。 Array#concat()
returns the updated array so that is the return for reduce()
. Array#concat()
返回更新后的数组,因此是reduce()
的返回值。
Or use spread to return new array [...accumulator, selsected ? i :-1]
还是使用传播返回新数组
[...accumulator, selsected ? i :-1]
[...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>
If you want indexes with selected
= true, then you could do something like this using reduce
. 如果要使用
selected
= true的索引,则可以使用reduce
类似的操作。 If the current item in context has selected
=== true, add the index to the accumulator, else return the accumulator. 如果上下文中的当前项已
selected
=== true,则将索引添加到累加器,否则返回累加器。 Currently, you're adding -1
if the condition is false
当前,如果条件为
false
,则您要添加-1
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)
If you want the whole object, then you can simply use filter
: 如果需要整个对象,则可以简单地使用
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)
Using Array#reduce
and the ...
spread operator keep the existing indexes and add the new index if selected
is true
. 如果
selected
是true
则使用Array#reduce
和...
spread运算符保留现有索引并添加新索引。
let options = [ {label: 'foo1', selected: false}, {label: 'foo2', selected: true}, {label: 'foo2', selected: false}, {label: 'foo2', selected: false}, {label: 'foo2', selected: false}, {label: 'foo2', selected: true}, {label: 'foo2', selected: 1} ]; const obj = options.reduce((acc, currentValue, index) => { // checking only for boolean true not for truthy values return acc = currentValue["selected"] === true ? [...acc, index] : acc; }, []); console.log(obj);
Another way is use of push
instead of concat
: 另一种方法是使用
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.