此问题已在此处找到答案:
为什么在数组迭代中使用“for…in”是个坏主意((27个答案)
三天前关门。
我正在处理一组对象(名为 emails
并从一个名为emails.json的文件中导入,每个文件代表一封电子邮件。我正在尝试创建新的数组来保存属于垃圾箱或收件箱的内容。他们是一个或另一个。这是一系列电子邮件:
[{"id":1,"from":"Inna Leah","email":"ileah0@goo.gl","received":"2020-11-06T13:16:38Z","subject":"Way of the Dragon, The (a.k.a. Return of the Dragon) (Meng long guo jiang)","content":"Quisque porta volutpat erat. Quisque erat eros, viverra eget, congue eget, semper rutrum, nulla. Nunc purus.\n\nPhasellus in felis. Donec semper sapien a libero. Nam dui.\n\nProin leo odio, porttitor id, consequat in, consequat ut, nulla. Sed accumsan felis. Ut at dolor quis odio consequat varius.","trash":true},
{"id":2,"from":"Elana Snufflebottom","email":"esnufflebottom1@e-recht24.de","received":"2020-10-08T01:18:13Z","subject":"Black Hawk Down","content":"Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Vivamus vestibulum sagittis sapien. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.\n\nEtiam vel augue. Vestibulum rutrum rutrum neque. Aenean auctor gravida sem.","trash":true},
{"id":3,"from":"Corbett Durram","email":"cdurram2@pinterest.com","received":"2021-03-24T07:17:54Z","subject":"Backstairs (Hintertreppe)","content":"Duis aliquam convallis nunc. Proin at turpis a pede posuere nonummy. Integer non velit.\n\nDonec diam neque, vestibulum eget, vulputate ut, ultrices vel, augue. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Donec pharetra, magna vestibulum aliquet ultrices, erat tortor sollicitudin mi, sit amet lobortis sapien sapien non mi. Integer ac neque.","trash":true},
{"id":4,"from":"Rockwell Leakner","email":"rleakner3@zdnet.com","received":"2021-04-15T22:39:18Z","subject":"All at Sea","content":"Curabitur gravida nisi at nibh. In hac habitasse platea dictumst. Aliquam augue quam, sollicitudin vitae, consectetuer eget, rutrum at, lorem.\n\nInteger tincidunt ante vel ipsum. Praesent blandit lacinia erat. Vestibulum sed magna at nunc commodo placerat.\n\nPraesent blandit. Nam nulla. Integer pede justo, lacinia eget, tincidunt eget, tempus vel, pede.","trash":true},
{"id":5,"from":"Warde Horribine","email":"whorribine4@multiply.com","received":"2020-07-06T12:38:24Z","subject":"Julia","content":"Duis bibendum. Morbi non quam nec dui luctus rutrum. Nulla tellus.\n\nIn sagittis dui vel nisl. Duis ac nibh. Fusce lacus purus, aliquet at, feugiat non, pretium quis, lectus.\n\nSuspendisse potenti. In eleifend quam a odio. In hac habitasse platea dictumst.","trash":true},
{"id":6,"from":"Catie Foulstone","email":"cfoulstone5@cornell.edu","received":"2020-11-01T20:53:40Z","subject":"Contracted","content":"Duis bibendum, felis sed interdum venenatis, turpis enim blandit mi, in porttitor pede justo eu massa. Donec dapibus. Duis at velit eu est congue elementum.","trash":false},
{"id":7,"from":"Adoree Seeking","email":"aseeking6@cyberchimps.com","received":"2021-01-31T13:41:54Z","subject":"Mackenna's Gold","content":"Cras mi pede, malesuada in, imperdiet et, commodo vulputate, justo. In blandit ultrices enim. Lorem ipsum dolor sit amet, consectetuer adipiscing elit.\n\nProin interdum mauris non ligula pellentesque ultrices. Phasellus id sapien in sapien iaculis congue. Vivamus metus arcu, adipiscing molestie, hendrerit at, vulputate vitae, nisl.\n\nAenean lectus. Pellentesque eget nunc. Donec quis orci eget orci vehicula condimentum.","trash":true},
{"id":8,"from":"Sandie Clamp","email":"sclamp7@nymag.com","received":"2020-08-28T11:35:41Z","subject":"Go Now","content":"Nullam porttitor lacus at turpis. Donec posuere metus vitae ipsum. Aliquam non mauris.\n\nMorbi non lectus. Aliquam sit amet diam in magna bibendum imperdiet. Nullam orci pede, venenatis non, sodales sed, tincidunt eu, felis.","trash":true},
{"id":9,"from":"Nelly Malkin","email":"nmalkin8@nifty.com","received":"2020-09-21T07:15:13Z","subject":"Thing, The","content":"Integer tincidunt ante vel ipsum. Praesent blandit lacinia erat. Vestibulum sed magna at nunc commodo placerat.\n\nPraesent blandit. Nam nulla. Integer pede justo, lacinia eget, tincidunt eget, tempus vel, pede.\n\nMorbi porttitor lorem id ligula. Suspendisse ornare consequat lectus. In est risus, auctor sed, tristique in, tempus sit amet, sem.","trash":false},
{"id":10,"from":"Clari Sollam","email":"csollam9@twitter.com","received":"2021-05-15T03:11:37Z","subject":"Very Harold & Kumar 3D Christmas, A","content":"Nullam porttitor lacus at turpis. Donec posuere metus vitae ipsum. Aliquam non mauris.\n\nMorbi non lectus. Aliquam sit amet diam in magna bibendum imperdiet. Nullam orci pede, venenatis non, sodales sed, tincidunt eu, felis.\n\nFusce posuere felis sed lacus. Morbi sem mauris, laoreet ut, rhoncus aliquet, pulvinar sed, nisl. Nunc rhoncus dui vel sem.","trash":true}]
但是,当我尝试以下代码时,我的目标是用垃圾设置为false的所有电子邮件对象填充我的“收件箱”数组,但没有实现,因为我的“收件箱”数组是空的:
const inbox = [];
for (const email in emails) {
if (email['trash'] === false) {
inbox.push(email);
console.log(inbox.length);
}
}
我相信我可以在单独的文件中访问该对象,因为以下代码确实可以工作并复制整个阵列:
const copy = [];
emails.map((email) => (
copy.push(email)
));
3条答案
按热度按时间gojuced71#
当你使用
for (const email in emails)
电子邮件是0,1,...
.简单地说,你可以使用
forEach
:91zkwejq2#
你可以用
forEach
在…上emails
数组。zzzyeukh3#
下面是一个使用
forEach
```const arr = [{"id":1,"from":"Inna Leah","email":"ileah0@goo.gl","received":"2020-11-06T13:16:38Z","subject":"Way of the Dragon, The (a.k.a. Return of the Dragon) (Meng long guo jiang)","content":"Quisque porta volutpat erat. Quisque erat eros, viverra eget, congue eget, semper rutrum, nulla. Nunc purus.\n\nPhasellus in felis. Donec semper sapien a libero. Nam dui.\n\nProin leo odio, porttitor id, consequat in, consequat ut, nulla. Sed accumsan felis. Ut at dolor quis odio consequat varius.","trash":true},
{"id":2,"from":"Elana Snufflebottom","email":"esnufflebottom1@e-recht24.de","received":"2020-10-08T01:18:13Z","subject":"Black Hawk Down","content":"Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Vivamus vestibulum sagittis sapien. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.\n\nEtiam vel augue. Vestibulum rutrum rutrum neque. Aenean auctor gravida sem.","trash":true},
{"id":3,"from":"Corbett Durram","email":"cdurram2@pinterest.com","received":"2021-03-24T07:17:54Z","subject":"Backstairs (Hintertreppe)","content":"Duis aliquam convallis nunc. Proin at turpis a pede posuere nonummy. Integer non velit.\n\nDonec diam neque, vestibulum eget, vulputate ut, ultrices vel, augue. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Donec pharetra, magna vestibulum aliquet ultrices, erat tortor sollicitudin mi, sit amet lobortis sapien sapien non mi. Integer ac neque.","trash":true},
{"id":4,"from":"Rockwell Leakner","email":"rleakner3@zdnet.com","received":"2021-04-15T22:39:18Z","subject":"All at Sea","content":"Curabitur gravida nisi at nibh. In hac habitasse platea dictumst. Aliquam augue quam, sollicitudin vitae, consectetuer eget, rutrum at, lorem.\n\nInteger tincidunt ante vel ipsum. Praesent blandit lacinia erat. Vestibulum sed magna at nunc commodo placerat.\n\nPraesent blandit. Nam nulla. Integer pede justo, lacinia eget, tincidunt eget, tempus vel, pede.","trash":true},
{"id":5,"from":"Warde Horribine","email":"whorribine4@multiply.com","received":"2020-07-06T12:38:24Z","subject":"Julia","content":"Duis bibendum. Morbi non quam nec dui luctus rutrum. Nulla tellus.\n\nIn sagittis dui vel nisl. Duis ac nibh. Fusce lacus purus, aliquet at, feugiat non, pretium quis, lectus.\n\nSuspendisse potenti. In eleifend quam a odio. In hac habitasse platea dictumst.","trash":true},
{"id":6,"from":"Catie Foulstone","email":"cfoulstone5@cornell.edu","received":"2020-11-01T20:53:40Z","subject":"Contracted","content":"Duis bibendum, felis sed interdum venenatis, turpis enim blandit mi, in porttitor pede justo eu massa. Donec dapibus. Duis at velit eu est congue elementum.","trash":false},
{"id":7,"from":"Adoree Seeking","email":"aseeking6@cyberchimps.com","received":"2021-01-31T13:41:54Z","subject":"Mackenna's Gold","content":"Cras mi pede, malesuada in, imperdiet et, commodo vulputate, justo. In blandit ultrices enim. Lorem ipsum dolor sit amet, consectetuer adipiscing elit.\n\nProin interdum mauris non ligula pellentesque ultrices. Phasellus id sapien in sapien iaculis congue. Vivamus metus arcu, adipiscing molestie, hendrerit at, vulputate vitae, nisl.\n\nAenean lectus. Pellentesque eget nunc. Donec quis orci eget orci vehicula condimentum.","trash":true},
{"id":8,"from":"Sandie Clamp","email":"sclamp7@nymag.com","received":"2020-08-28T11:35:41Z","subject":"Go Now","content":"Nullam porttitor lacus at turpis. Donec posuere metus vitae ipsum. Aliquam non mauris.\n\nMorbi non lectus. Aliquam sit amet diam in magna bibendum imperdiet. Nullam orci pede, venenatis non, sodales sed, tincidunt eu, felis.","trash":true},
{"id":9,"from":"Nelly Malkin","email":"nmalkin8@nifty.com","received":"2020-09-21T07:15:13Z","subject":"Thing, The","content":"Integer tincidunt ante vel ipsum. Praesent blandit lacinia erat. Vestibulum sed magna at nunc commodo placerat.\n\nPraesent blandit. Nam nulla. Integer pede justo, lacinia eget, tincidunt eget, tempus vel, pede.\n\nMorbi porttitor lorem id ligula. Suspendisse ornare consequat lectus. In est risus, auctor sed, tristique in, tempus sit amet, sem.","trash":false},
{"id":10,"from":"Clari Sollam","email":"csollam9@twitter.com","received":"2021-05-15T03:11:37Z","subject":"Very Harold & Kumar 3D Christmas, A","content":"Nullam porttitor lacus at turpis. Donec posuere metus vitae ipsum. Aliquam non mauris.\n\nMorbi non lectus. Aliquam sit amet diam in magna bibendum imperdiet. Nullam orci pede, venenatis non, sodales sed, tincidunt eu, felis.\n\nFusce posuere felis sed lacus. Morbi sem mauris, laoreet ut, rhoncus aliquet, pulvinar sed, nisl. Nunc rhoncus dui vel sem.","trash":true}];
const inbox = [];
arr.forEach(email => {
if (email['trash'] === false) {
inbox.push(email);
console.log(inbox.length);
}
});