首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何过滤重复的key?

问如何过滤重复的key?
EN

Stack Overflow用户
提问于 2020-07-02 05:33:07
回答 3查看 36关注 0票数 0
代码语言:javascript
复制
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的数组?

EN

回答 3

Stack Overflow用户

发布于 2020-07-02 05:35:30

您可以结合使用Array#findIndex和Array#filter。

代码语言:javascript
复制
const res = bible.filter((x,idx)=>bible.findIndex(y=>y.Book===x.Book)===idx);

​

代码语言:javascript
复制
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);

​

票数 1
EN

Stack Overflow用户

发布于 2020-07-02 05:37:49

​

代码语言:javascript
复制
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);

​

票数 1
EN

Stack Overflow用户

发布于 2020-07-02 05:44:48

使用map获取图书列表,并使用Set为您删除重复的内容

​

代码语言:javascript
复制
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

​

代码语言:javascript
复制
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);

​

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62685666

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档