const bible = [
{
"id":1001001,
"Book":"Genesis",
"Chapter":1,
"Verse":1,
"Text":"In the beginning God created the heaven and the earth."
},
{
"id":1001002,
"Book":"Genesis",
"Chapter":1,
"Verse":2,
"Text":"And the earth was without form, and void; and darkness was upon the face of the deep. And the Spirit of God moved upon the face of the waters."
},
{
"id":1001003,
"Book":"Genesis",
"Chapter":1,
"Verse":3,
"Text":"And God said, Let there be light: and there was light."
},
{
"id":1001004,
"Book":"Exodus",
"Chapter":1,
"Verse":4,
"Text":"And God saw the light, that it was good: and God divided the light from the darkness."
},
{
"id":1001005,
"Book":"Exodus",
"Chapter":1,
"Verse":5,
"Text":"And God called the light Day, and the darkness he called Night. And the evening and the morning were the first day."
},
{
"id":1001006,
"Book":"Genesis",
"Chapter":1,
"Verse":6,
"Text":"And God said, Let there be a firmament in the midst of the waters, and let it divide the waters from the waters."
},
{
"id":1001007,
"Book":"Revelation",
"Chapter":1,
"Verse":7,
"Text":"And God made the firmament, and divided the waters which were under the firmament from the waters which were above the firmament: and it was so."
},
{
"id":1001008,
"Book":"Revelation",
"Chapter":1,
"Verse":8,
"Text":"And God called the firmament Heaven. And the evening and the morning were the second day."
},
{
"id":1001009,
"Book":"Genesis",
"Chapter":1,
"Verse":9,
"Text":"And God said, Let the waters under the heaven be gathered together unto one place, and let the dry land appear: and it was so."
},
{
"id":1001010,
"Book":"Genesis",
"Chapter":1,
"Verse":10,
"Text":"And God called the dry land Earth; and the gathering together of the waters called he Seas: and God saw that it was good."
}
]如何返回没有重复bible.Book的数组?
发布于 2020-07-02 05:35:30
您可以结合使用Array#findIndex和Array#filter。
const res = bible.filter((x,idx)=>bible.findIndex(y=>y.Book===x.Book)===idx);
const bible = [
{
"id":1001001,
"Book":"Genesis",
"Chapter":1,
"Verse":1,
"Text":"In the beginning God created the heaven and the earth."
},
{
"id":1001002,
"Book":"Genesis",
"Chapter":1,
"Verse":2,
"Text":"And the earth was without form, and void; and darkness was upon the face of the deep. And the Spirit of God moved upon the face of the waters."
},
{
"id":1001003,
"Book":"Genesis",
"Chapter":1,
"Verse":3,
"Text":"And God said, Let there be light: and there was light."
},
{
"id":1001004,
"Book":"Exodus",
"Chapter":1,
"Verse":4,
"Text":"And God saw the light, that it was good: and God divided the light from the darkness."
},
{
"id":1001005,
"Book":"Exodus",
"Chapter":1,
"Verse":5,
"Text":"And God called the light Day, and the darkness he called Night. And the evening and the morning were the first day."
},
{
"id":1001006,
"Book":"Genesis",
"Chapter":1,
"Verse":6,
"Text":"And God said, Let there be a firmament in the midst of the waters, and let it divide the waters from the waters."
},
{
"id":1001007,
"Book":"Revelation",
"Chapter":1,
"Verse":7,
"Text":"And God made the firmament, and divided the waters which were under the firmament from the waters which were above the firmament: and it was so."
},
{
"id":1001008,
"Book":"Revelation",
"Chapter":1,
"Verse":8,
"Text":"And God called the firmament Heaven. And the evening and the morning were the second day."
},
{
"id":1001009,
"Book":"Genesis",
"Chapter":1,
"Verse":9,
"Text":"And God said, Let the waters under the heaven be gathered together unto one place, and let the dry land appear: and it was so."
},
{
"id":1001010,
"Book":"Genesis",
"Chapter":1,
"Verse":10,
"Text":"And God called the dry land Earth; and the gathering together of the waters called he Seas: and God saw that it was good."
}
];
const res = bible.filter((x,idx)=>bible.findIndex(y=>y.Book===x.Book)===idx);
console.log(res);
发布于 2020-07-02 05:37:49
const bible = [
{
"id":1001001,
Book:"Genesis",
"Chapter":1,
"Verse":1,
"Text":"In the beginning God created the heaven and the earth."
},
{
"id":1001002,
Book:"Genesis",
"Chapter":1,
"Verse":2,
"Text":"And the earth was without form, and void; and darkness was upon the face of the deep. And the Spirit of God moved upon the face of the waters."
},
{
"id":1001003,
Book:"Genesis",
"Chapter":1,
"Verse":3,
"Text":"And God said, Let there be light: and there was light."
},
{
"id":1001004,
Book:"Exodus",
"Chapter":1,
"Verse":4,
"Text":"And God saw the light, that it was good: and God divided the light from the darkness."
},
{
"id":1001005,
Book:"Exodus",
"Chapter":1,
"Verse":5,
"Text":"And God called the light Day, and the darkness he called Night. And the evening and the morning were the first day."
},
{
"id":1001006,
Book:"Genesis",
"Chapter":1,
"Verse":6,
"Text":"And God said, Let there be a firmament in the midst of the waters, and let it divide the waters from the waters."
},
{
"id":1001007,
Book:"Revelation",
"Chapter":1,
"Verse":7,
"Text":"And God made the firmament, and divided the waters which were under the firmament from the waters which were above the firmament: and it was so."
},
{
"id":1001008,
Book:"Revelation",
"Chapter":1,
"Verse":8,
"Text":"And God called the firmament Heaven. And the evening and the morning were the second day."
},
{
"id":1001009,
Book:"Genesis",
"Chapter":1,
"Verse":9,
"Text":"And God said, Let the waters under the heaven be gathered together unto one place, and let the dry land appear: and it was so."
},
{
"id":1001010,
Book:"Genesis",
"Chapter":1,
"Verse":10,
"Text":"And God called the dry land Earth; and the gathering together of the waters called he Seas: and God saw that it was good."
}
];
const filtered = bible.filter((book, index, bible) => !bible.some((book2, index2) => (book.Book === book2.Book) && (index > index2)));
console.log(filtered);
发布于 2020-07-02 05:44:48
使用map获取图书列表,并使用Set为您删除重复的内容
var bible = [
{ Book: 'Genesis'},
{ Book: 'Exodus'},
{ Book: 'Revelation'},
{ Book: 'Genesis'},
{ Book: 'Exodus'},
{ Book: 'Revelation'},
{ Book: 'Genesis'},
{ Book: 'Exodus'},
{ Book: 'Revelation'},
]
const books = [...new Set(bible.map(b => b.Book))];
console.log(books);
或者,您可以在Object.keys中使用reduce
var bible = [
{ Book: 'Genesis'},
{ Book: 'Exodus'},
{ Book: 'Revelation'},
{ Book: 'Genesis'},
{ Book: 'Exodus'},
{ Book: 'Revelation'},
{ Book: 'Genesis'},
{ Book: 'Exodus'},
{ Book: 'Revelation'},
]
const books = Object.keys(bible.reduce((o, item) => (o[item.Book] = 1, o), {}));
console.log(books);
https://stackoverflow.com/questions/62685666
复制相似问题