How to Select One Object from the List of Objects

Select some objects from a list of objects based on list of one field

final ArrayList<MyNamedObject> namedObjectList = ...;
final List<String> nameList = ...;

Use

namedObjectList.stream()
.filter((final MyNamedObject o) -> nameList.stream().anyMatch((final String name) -> Objects.equals(name, o.getName())))
.collect(Collectors.toList());

If there are some duplicate values in the name list, either keep the list but use .distinct():

namedObjectList.stream()
.filter((final MyNamedObject o) -> nameList.stream().distinct().anyMatch((final String name) -> Objects.equals(name, o.getName())))
.collect(Collectors.toList());

or create a Set:

final Set<String> nameSet = nameList.stream()
.distinct()
.collect(Collectors.toSet());
namedObjectList.stream()
.filter((final MyNamedObject o) -> nameSet.stream().anyMatch((final String name) -> Objects.equals(name, o.getName())))
.collect(Collectors.toList());

If nameList or nameSet can contain null, use .filter(Objects::nonNull). If neither name nor o.getName() can be null, replace Objects.equals(name, o.getName()) by name.equals(o.getName()).

select some objects from object list through for loop based on a condition

You can use filter in computed property:

Vue.component('Child', {
template: `
<div>
<div v-for="(guest, g) in myGuests" :key="g">
{{ guest.name }} - {{ guest.age }}
</div>
</div>
`,
props: {
guests: {
type: Array,
default: null,
},
},
computed: {
myGuests() {
return this.guests.filter(g => g.age > 18)
}
}
})

new Vue({
el: '#demo',
data() {
return {
guests: [{'name': 'aa', 'age': 15}, {'name': 'bb', 'age': 21}, {'name': 'cc', 'age': 18}, {'name': 'dd', 'age': 19}, {'name': 'ee', 'age': 20}, {'name': 'ff', 'age': 24}]
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="demo">
<child :guests="guests"> </child>
</div>

how to select an object from a list of objects by its attribute in python

Try

dave = next(person for person in a.pList if person.num == 123)

or

for person in a.pList:
if person.num == 123:
break
else:
print "Not found."
dave = person

How to find a single item in a list of objects?

FileCabinet fileCabinet = fileCabinets.SingleOrDefault(x => x.(propertyName) == (valueToMatch));

Like @Rainman said, if there is a probability that the name is not unique, use his solution. If you are sure that the Name property is unique, use this one.

Select a property from an array of objects based on a value : Javascript

You can use reduce and check if the checked property is true, then push (As pointed out by assoron) the value to the accumulator - there is no need for 2 loops:

const arr = [  { "value": "abc", "checked": true },  { "value": "xyz", "checked": false },  { "value": "lmn", "checked": true }]
const filtered = arr.reduce((a, o) => (o.checked && a.push(o.value), a), []) console.log(filtered)

Selecting objects with list of other objects using LINQ

Your requirements essentially necessitate creating a new Team object with a reduced set of players (or alternatively permanently removing the younger players from the original Team object).

var teamsEnglandOnlyOver28Years =
teams
// Only teams from England
.Where(t => t.Country == "England")
// Create a new instance with only the older players
.Select(t => new Team()
{
TeamName = t.TeamName,
StadiumName = t.StadiumName,
Players = t.Players.Where(p => p.Age > 28).ToList(),
Country = t.Country
});

This may or may not be what you actually intend. It seems a bit awkward because, for example, the team name is the same as for the original team but the roster is reduced.



Related Topics



Leave a reply



Submit