问题
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