How to read from a file & store key:value pair in String Array?

大憨熊 提交于 2020-01-25 11:31:46

问题


I have a coffee script file with the following data in it. I want to create a string array that will store the following data as key:value pair in it.

abTests:
    productRanking:
      version: 4
      groups: [
        ratio:
          default: 1
          us: 0.90
          me: 0.0
        value: "LessPopularityEPC"
      ,
        ratio:
          default: 0
          us: 0.1
        value: "CtrEpcJob"
      ,
        ratio:
          default: 0
          me: 1.0
        value: "RandomPerVisitor"
      ]
    sabt:
      version: 1
      groups: [
        ratio:
          default: 1
          us: 0.90
        value: "default"
      ,
        ratio:
          default: 0
          us: 0.05
        value: "colorBoost"
      ,
        ratio:
          default: 0
          us: 0.05
        value: "colorPriority"
      ,
        ratio:
          default: 0
          us: 0
        value: "noColorClause"
      ]

I want to create a String Array with these data in the following format

productRanking:LessPopularityEPC
productRanking:CtrEpcJob
productRanking:RandomPerVisitor
sabt:default
sabt:colorboost
sabt:colorPriority
sabt:nocolorClause

Is there any way to solve this problem??


回答1:


If by String Array you mean this

['productRanking:LessPopularityEPC', 'productRanking:CtrEpcJob', 'productRanking:RandomPerVisitor']

You can do that with the following coffeescript code

data = abTests:
  ...

array = []
for testName,tests of data['abTests']
  for categoryName,categoryElems of tests['groups']
    array.push (testName + ':' + categoryElems['value'])

console.log array
#=> ['productRanking:LessPopularityEPC', 'productRanking:CtrEpcJob', 'productRanking:RandomPerVisitor']


来源:https://stackoverflow.com/questions/35517517/how-to-read-from-a-file-store-keyvalue-pair-in-string-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!