简体   繁体   English

是否可以根据键的一部分过滤键值对?

[英]Is it possible to filter key value pairs based on a section of the key?

I am building a react app with Firebase back end.我正在构建一个后端为 Firebase 的 React 应用程序。 My Firestore collection contains data in the format of:我的 Firestore 集合包含以下格式的数据:

{
  'Title One': 'The Title Here',
  'Title One Score': '23',
  'Title Two': 'The Second Title Here',
  'Title Two Score': '43',
  'Title Three': 'The Third Title Here',
  'Title Three Score': '65',
}

I would like to filter out all the fields that end with the 'Score' and then add up all the values of the score.我想过滤掉所有以“分数”结尾的字段,然后将分数的所有值相加。 Like in that case, I would get 131.就像那样,我会得到 131。

I was trying along these lines but didn't know how to proceed我正在尝试沿着这些路线但不知道如何进行

const totalScore = Object.entries(datasets)
.filter(([key, value]) => [
  'Score'
].includes(key))
.sort()
.map(pair => {
  const key = pair[0]
  const value = pair[1]
  //GET THE TOTAL SCORES HERE
  
  return(
    // <h2>{ TOTAL SCORE }</h2>
  )
})

Does any one know how to do that?有谁知道这是怎么做到的吗? Please help.请帮忙。

Thanks.谢谢。

You could get the entries and check the key for the wanted ending.您可以获得条目并检查所需结尾的密钥。 If it has the wanted ending add the value or take zero for getting a total value.如果它有想要的结局,则添加该值或取零以获得总值。

 const data = { 'Title One': 'The Title Here', 'Title One Score': '23', 'Title Two': 'The Second Title Here', 'Title Two Score': '43', 'Title Three': 'The Third Title Here', 'Title Three Score': '65' }, total = Object.entries(data).reduce((t, [k, v]) => t + (k.endsWith(' Score')? +v: 0), 0); console.log(total);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在redshift中提取具有相同键的键值对 - Extract key value pairs with the same key in redshift Amazon Textract - 如何定义我的键值对 - Amazon Textract - How to define my key-value pairs 如何使用 snapshotChanges() 方法获取键值和过滤数据? - How to use snapshotChanges() method to get both key value and filter the data? StandardSQL BigQuery 值从一个数组中的键值对到单独的列中。 如何? - StandardSQL BigQuery values from key:value pairs within an Array into separate columns. How? 在哪里放置 Cloudfront 密钥对以进行 Wordpress 身份验证 - Where to place Cloudfront Key Pairs for Wordpress Authentication 如何创建具有多个密钥对的 EC2 实例? - How do you create an EC2 instance with multiple key pairs? 为什么不按部分实体键过滤? - Why not filter by part of entity key? 在 DynamoDB 中使用 json 作为排序键/分区键值的好习惯吗? - Is using json as sort key/partition key value good practice in DynamoDB? 获取对象数组中键值的总和 - Get SUM of value of key inside an array of objects 将新的键值对添加到现有的 Firebase - Add new key value pair to existing Firebase
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM