打字稿中的Json格式(角度7)

时间:2019-05-17 04:06:27

标签: javascript json ecmascript-6

我有一个嵌套的json,如下所示。

print ("Note: you must answer using A, B, C or D")
print (" ")

lives = 3
print ("lives =", lives)
print (" ")

name = input ("What is your name?")
print ("Hello",name,". Good Luck.")


while lives != 0:
    ##is the player ready to start
    play = input ("Would you like to start? (Type Y for yes and N for no)")
    if play == "Y" or play == "y":
        from time import sleep
        sleep (1.0)
        print("Starting in...")
        sleep (1.0)
        print("3")
        sleep(1.0)
        print("2")
        sleep(1.0)
        print("1")
        break
    ##3, 2, 1 countdown added wk.4 friday
    elif play == "N" or play == "n":
        print ("End")
        break
    else:
        print ("That is not an answer.\n")


## 1st Question
question1 = ("1. What is Brisbanes AFL team called?")
options1 = (" a. Brisbane Tigers \n b. Brisbane Lions \n c. Brisbane Broncos \n d. Brisbane Magpies")
print (question1)
print (options1)
answer = input (">")
if answer == "B" or answer == "b":
    print ("Correct!")
else:
    print("Incorrect.")
    print ("lives =", lives - 1)
    lives=lives-1


## 2nd Question
question2 = ("2. What sport did Greg Inglis play")
options2 = (" a. rugby league \n b. rugby union \n c. AFL \n d. Soccer")
print (question2)
print (options2)
answer = input (">")
if answer == "A" or answer == "a":
    print ("Correct!")
else:
    print("Incorrect.")
    print ("lives =", lives - 1)
    lives=lives-1
## 3rd Question
question3 = ("3. Where were the 2018 Commonwealth Games held?")
options3 = (" a. Sunshine Coast \n b. Melbourne \n c. Brsbane\n d. Gold coast")
print (question3)
print (options3)
answer = input (">")
if answer == "D" or answer == "d":
    print ("Correct!")
else:
    print("Incorrect.")
    print ("lives =", lives - 1)
    lives=lives-1


## 4th Question
question4 = ("4. How many players in a netball team can shoot?")
options4 = (" a. 1 \n b. 2 \n c. 3\n d. 4")
print (question4)
print (options4)
answer = input (">")
if answer == "B" or answer == "b":
    print ("Correct!")
else:
    print("Incorrect.")
    print ("lives =", lives - 1)
    lives=lives-1


我需要根据以下表格对详细信息进行分组。

{
card:[{
    id:1;
    details:[{id:1},{id:2}],
    sheet:{
        id:1
    }
},{
    id:2;
    details:[{id:3},{id:4}],
    sheet:{
        id:1
    }
}
]
}

如何在打字稿中实现这一目标?

经历了NPM-groupBy,但我认为这并不能真正解决我的问题。 还有其他方法吗?

1 个答案:

答案 0 :(得分:0)

如果我对您的理解正确,那么这将是数组reduce方法的好用例,例如:

const cards = [
  {
    id: 1,
    details: [
      {id:1},
      {id:2}
    ],
    sheet:{
      id:1
    }
  },
  {
    id:2,
    details: [
      {id:3},
      {id:4}
    ],
    sheet:{
      id:1
    }
  },
  {
    id:3,
    details: [
      {id:5},
      {id:6}
    ],
    sheet:{
      id:2
    }
  }
];

const sheets = cards.reduce( (sheets: Array<any>, card: any) => {
  const existingSheet = sheets.find(sheet => card.sheet.id === sheet.id);
    if (existingSheet) {
      existingSheet.details.concat(card.details);
    } else {
      sheets.push({id: card.sheet.id, details: card.details});
    }
    return sheets;
  }, []
);

console.log(sheets);