admin管理员组文章数量:1356294
I want to extract a collection of objects from an array based on their ID. I am using vanilla Javascript.
contacts = [
{ID: 1, firstName: "Aaron", lastName: "Smith"},
{ID: 2, firstName: "Ben", lastName: "Smith"},
{ID: 3, firstName: "Conrad", lastName: "Smith"}
];
filteredContactIds = [1, 3];
filteredContacts = [];
filteredContactIds.forEach(function (filteredId) {
filteredContacts.push(
contacts.forEach(function (contact) {
if (contact.ID == filteredId) {
return contact;
}
})
)
});
Contacts and filteredContactIds are simplified, in my code both arrays are both populated correctly. The problem is, the filteredContacts array is only receiving the ID property of each contact object, I want to insert the whole object.
I have exhausted my limited understanding. Can anyone point out the problem?
I want to extract a collection of objects from an array based on their ID. I am using vanilla Javascript.
contacts = [
{ID: 1, firstName: "Aaron", lastName: "Smith"},
{ID: 2, firstName: "Ben", lastName: "Smith"},
{ID: 3, firstName: "Conrad", lastName: "Smith"}
];
filteredContactIds = [1, 3];
filteredContacts = [];
filteredContactIds.forEach(function (filteredId) {
filteredContacts.push(
contacts.forEach(function (contact) {
if (contact.ID == filteredId) {
return contact;
}
})
)
});
Contacts and filteredContactIds are simplified, in my code both arrays are both populated correctly. The problem is, the filteredContacts array is only receiving the ID property of each contact object, I want to insert the whole object.
I have exhausted my limited understanding. Can anyone point out the problem?
Share Improve this question asked Nov 24, 2017 at 22:40 Ben E.Ben E. 10.6k4 gold badges16 silver badges10 bronze badges1 Answer
Reset to default 6The problem with your approach is that Array#forEach
doesn't return a value. It just performs some operation(s) on each item in an array. This means undefined
is being pushed to filteredContacts
. You could use contacts.filter
but you can remove the inner loop.
Using Array#filter
and an array containment check:
const contacts = [
{ID: 1, firstName: "Aaron", lastName: "Smith"},
{ID: 2, firstName: "Ben", lastName: "Smith"},
{ID: 3, firstName: "Conrad", lastName: "Smith"}
];
const filteredContactIds = [1, 3];
const filteredContacts = contacts.filter(({ ID }) => filteredContactIds.includes(ID));
console.log(filteredContacts);
Instead of having to have two loops, one single run through the contacts
array will work. This iterates through the array and checks whether the current contact's ID is inside the filteredContactIds
array, removing another loop. Here's a desugared version for ES5 and below:
var contacts = [
{ID: 1, firstName: "Aaron", lastName: "Smith"},
{ID: 2, firstName: "Ben", lastName: "Smith"},
{ID: 3, firstName: "Conrad", lastName: "Smith"}
];
var filteredContactIds = [1, 3];
var filteredContacts = contacts.filter(function(contact) {
return filteredContactIds.indexOf(contact.ID) > -1;
});
console.log(filteredContacts);
Instead of Array#includes
, it uses Array#indexOf
, and does not use object destructuring nor arrow functions nor const
.
本文标签: Javascript Pushing an object to an array from a foreachStack Overflow
版权声明:本文标题:Javascript: Pushing an object to an array from a foreach - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744022960a2577541.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论