简体   繁体   中英

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

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??

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']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM