admin管理员组文章数量:1220350
I'm traversing through an object array:
people = [
{id:1, name:"Bob", available:false},
{id:2, name:"Sally", available:true},
{id:1, name:"Trish", available:false},
]
I want my output to be the names of those available:
["Sally"]
I currently know how to map and extract for a field. How do I add the conditional?
const peopleAvailable = people.map(person => person.value);
Want to do something like this:
const peopleAvailable = people.map(person.available => person.value);
I'm traversing through an object array:
people = [
{id:1, name:"Bob", available:false},
{id:2, name:"Sally", available:true},
{id:1, name:"Trish", available:false},
]
I want my output to be the names of those available:
["Sally"]
I currently know how to map and extract for a field. How do I add the conditional?
const peopleAvailable = people.map(person => person.value);
Want to do something like this:
const peopleAvailable = people.map(person.available => person.value);
Share
Improve this question
edited Aug 28, 2019 at 21:25
Kamil Kiełczewski
92.4k34 gold badges394 silver badges370 bronze badges
asked Aug 28, 2019 at 21:01
lost9123193lost9123193
11k27 gold badges83 silver badges125 bronze badges
3 Answers
Reset to default 13You cannot conditionally map with the .map()
function alone, however you can use .filter()
to achieve what you require.
Calling filter will return a new array where each item in the new array satisfies your filtering criteria (ie people.available === true
).
In the case of your code, you can directly chain filter with your existing call to .map()
to obtain the desired result:
const people = [{
id: 1,
name: "Bob",
available: false
},
{
id: 2,
name: "Sally",
available: true
},
{
id: 1,
name: "Trish",
available: false
},
];
const peopleAvailable = people
.filter(people => people.available) /* filter available people */
.map(person => person.name) /* map each person to name */
console.log(peopleAvailable);
Try
people.reduce((a,c) => a.concat(c.available ? c.name:[]), [])
people = [
{id:1, name:"Bob", available:false},
{id:2, name:"Sally", available:true},
{id:1, name:"Trish", available:false},
];
let r = people.reduce((a,c) => a.concat(c.available ? c.name:[]), [])
console.log(r);
You can't check the condition in map()
. If you put conditions then other elements will be returns as undefined
. You can deal with this by applying filter()
which allows you to return only those elements which are falls in your condition. So you need to use filter()
then use map()
on the filtered result.
const array = [{
id: 1,
name: "Bob",
available: false
},
{
id: 2,
name: "Sally",
available: true
},
{
id: 1,
name: "Trish",
available: false
},
];
const withFilter = array
.filter((r) => { return r.available; })
.map((m) => { return m.name; });
console.log(withFilter);
const withOutFilter = array
.map((m) => { if(m.available) { return m.name; } });
console.log(withOutFilter);
本文标签: filterHow to map with a conditional in JavascriptStack Overflow
版权声明:本文标题:filter - How to map with a conditional in Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739328530a2158366.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论