【问题标题】:Removing other OBJECTs from an array of OBJECTs (NPM Twit)从 OBJECT 数组中删除其他 OBJECT (NPM Twit)
【发布时间】:2021-08-28 11:49:20
【问题描述】:

我正在为 Twitter 开发 NPM “twit”, 我正在尝试过滤来自tweet.text 的推文中的所有其他提及,除了第一次提及(回复)

tweet.entities.user_mentions 显示所有提及 tweet.text 可以包含所有提及和常规消息。

tweet.entities.user_mentions 输出类似user_mentions: [ [Object], [Object], [Object], [Object], [Object] ]

有什么建议可以删除除第一个之外的所有其他提及吗?来自tweet.text 如果它包含提及

tweet.entities.user_mentions.map((user) => user.screen_name)

数据示例

{
  created_at: 'Fri Jun 11 16:14:05 +0000 2021',
  id: redacted,
  id_str: 'redacted',
  text: '@redacted1 @redacted2 @redacted3 @redacted4 @redacted 5 bruh im busy working on my latest project kek',
  display_text_range: [ 54, 99 ],
  source: '<a href="https://mobile.twitter.com" rel="nofollow">Twitter Web App</a>',
  truncated: false,
  in_reply_to_status_id: redacted,
  in_reply_to_status_id_str: 'redacted',
  in_reply_to_user_id: redacted,
  in_reply_to_user_id_str: 'redacted',
  in_reply_to_screen_name: 'redacted1',
  user: {
    id: redacted,
    id_str: 'redacted',
    name: '᲼',
    screen_name: 'RudimentalHack',
    location: null,
    url: '',
    description: '',
    translator_type: 'none',
    protected: false,
    verified: false,
    followers_count: 4,
    friends_count: 25,
    listed_count: 0,
    favourites_count: 3,
    statuses_count: 88,
    created_at: 'Sat Mar 13 02:58:05 +0000 2021',
    utc_offset: null,
    time_zone: null,
    geo_enabled: false,
    lang: null,
    contributors_enabled: false,
    is_translator: false,
    profile_background_color: 'F5F8FA',
    profile_background_image_url: '',
    profile_background_image_url_https: '',
    profile_background_tile: false,
    profile_link_color: '1DA1F2',
    profile_sidebar_border_color: 'C0DEED',
    profile_sidebar_fill_color: 'DDEEF6',
    profile_text_color: '333333',
    profile_use_background_image: true,
    profile_image_url: 'http://pbs.twimg.com/profile_images/1396266004284575753/dzhuS7To_normal.png',
    profile_image_url_https: 'https://pbs.twimg.com/profile_images/1396266004284575753/dzhuS7To_normal.png',
    default_profile: true,
    default_profile_image: false,
    following: null,
    follow_request_sent: null,
    notifications: null,
    withheld_in_countries: []
  },
  geo: null,
  coordinates: null,
  place: null,
  contributors: null,
  is_quote_status: false,
  quote_count: 0,
  reply_count: 0,
  retweet_count: 0,
  favorite_count: 0,
  entities: {
    hashtags: [],
    urls: [],
    user_mentions: [ [Object], [Object], [Object], [Object], [Object] ],
    symbols: []
  },
  favorited: false,
  retweeted: false,
  filter_level: 'low',
  lang: 'en',
  timestamp_ms: '1623428045633'
}

【问题讨论】:

  • 如果您展示数据样本、描述您的预期输出并展示您在解决它时所做的尝试(在代码中),会更容易提供帮助
  • @Kinglish 我已经更新了它,所以它更好:)
  • 澄清一下,您想要取回相同的对象 - 除了使 user_mentions 成为单个对象(而不是多个对象的数组)和 text 只有一个(第一个)@ 987654331@?
  • @Kinglish 基本上是的。
  • 所以说文本只返回第一个@redacted1,然后是后面的消息,就像@redacted1 bruh im busy working on my latest project kek

标签: javascript node.js npm twitter


【解决方案1】:

这是一个简单的map(),它遍历对象。 .text 使用 reduce() 修改,.user_mentions 使用 splice() 修改。代码注释如下。

const twit = {
  created_at: 'Fri Jun 11 16:14:05 +0000 2021',
  id: 'redacted',
  id_str: 'redacted',
  text: '@redacted1 @redacted2 @redacted3 @redacted4 @redacted 5 bruh im busy working on my latest project kek',
  display_text_range: [54, 99],
  source: '<a href="https://mobile.twitter.com" rel="nofollow">Twitter Web App</a>',
  truncated: false,
  in_reply_to_status_id: 'redacted',
  in_reply_to_status_id_str: 'redacted',
  in_reply_to_user_id: 'redacted',
  in_reply_to_user_id_str: 'redacted',
  in_reply_to_screen_name: 'redacted1',
  user: {
    id: 'redacted',
    id_str: 'redacted',
    name: '᲼',
    screen_name: 'RudimentalHack',
    location: null,
    url: '',
    description: '',
    translator_type: 'none',
    protected: false,
    verified: false,
    followers_count: 4,
    friends_count: 25,
    listed_count: 0,
    favourites_count: 3,
    statuses_count: 88,
    created_at: 'Sat Mar 13 02:58:05 +0000 2021',
    utc_offset: null,
    time_zone: null,
    geo_enabled: false,
    lang: null,
    contributors_enabled: false,
    is_translator: false,
    profile_background_color: 'F5F8FA',
    profile_background_image_url: '',
    profile_background_image_url_https: '',
    profile_background_tile: false,
    profile_link_color: '1DA1F2',
    profile_sidebar_border_color: 'C0DEED',
    profile_sidebar_fill_color: 'DDEEF6',
    profile_text_color: '333333',
    profile_use_background_image: true,
    profile_image_url: 'http://pbs.twimg.com/profile_images/1396266004284575753/dzhuS7To_normal.png',
    profile_image_url_https: 'https://pbs.twimg.com/profile_images/1396266004284575753/dzhuS7To_normal.png',
    default_profile: true,
    default_profile_image: false,
    following: null,
    follow_request_sent: null,
    notifications: null,
    withheld_in_countries: []
  },
  geo: null,
  coordinates: null,
  place: null,
  contributors: null,
  is_quote_status: false,
  quote_count: 0,
  reply_count: 0,
  retweet_count: 0,
  favorite_count: 0,
  entities: {
    hashtags: [],
    urls: [],
    user_mentions: [{
      mention: 'hello'
    }, {
      mention: 'hello2'
    }, {
      mention: 'hello3'
    }, {
      mention: 'hello4'
    }, {
      mention: 'hello5'
    }],
    symbols: []
  },
  favorited: false,
  retweeted: false,
  filter_level: 'low',
  lang: 'en',
  timestamp_ms: '1623428045633'
}

// iterate twit as if it was an array 
const ntwit = [twit].map(el => {
  // fix the el.text by splitting and then testing our accumlating reduce array to see if it has an instance of @something
  el.text = el.text.split(" ").reduce((b, a) => {
    if (!a.includes('@') || !b.some(e => /\@(.)/g.test(e))) b.push(a)
    return b;
  }, []).join(" ");
  // simple splice using the array length as an argument
  el.entities.user_mentions.splice(1)
  return el
})[0]; // we faked it as an array, now just extract it.

console.log(ntwit);

【讨论】:

  • 今天晚些时候我会测试一下,我会在我测试时告诉你@Kinglish
  • 由于出现 IRL 问题,目前无法对其进行测试。但是当我有时间给自己一分钟时,我会测试 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-06
  • 2020-12-06
  • 2023-03-16
相关资源
最近更新 更多