Ich habe ein JSON-Array wie folgt:convert JSON Array zu ausgewählten Schlüssel JSON Array
[{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "[email protected]",
"address": {
"street":"201 S 4th St.",
"suite": "Apt. 556",
"city": "Gwenborough",
"zipcode": "92998-3874",
"geo": {
"lat": "-37.3159",
"lng": "81.1496"
}
},
"company": {
"name": "Romaguera-Crona",
"catchPhrase": "Multi-layered client-server neural-net",
"bs": "harness real-time e-markets"
},
"hobbies":[
{
"books":"fiction",
"sports":"football",
"music":"rock"
},
{
"books":"action",
"sports":"cricket",
"music":"jazz"
},
{
"books":"action",
"sports":"cricket",
"music":"cool"
},
]},{
"id":2,
"name": "Leanne Graham",
"username": "Bret",
"email": "[email protected]",
"address": {
"street": "Kulas Light",
"suite": "Apt. 556",
"city": "Gwenborough",
"zipcode": "92998-3874",
"geo": {
"lat": "-37.3159",
"lng": "81.1496"
}
},
"phone": ["1-770-736-8031 x56442", "4087917884", "4089088939"],
"website": "hildegard.org",
"dept":[{"name":"divya", "address":"abc"},{"name":"divya1", "address":"abc1"}],
"hobbies":[
{
"books":"fiction",
"sports":"football",
"music":"rock"
},
{
"books":"action",
"sports":"cricket",
"music":"jazz"
},
{
"books":"action",
"sports":"cricket",
"music":"cool"
},
]}]
ich nur bestimmte Schlüssel nur in Hobby Schlüssel verwenden wollte. Sagen wir, ich brauche nur Bücher und Sport von Hobbys. Wie kann ich das in Nodejs machen? Das Ergebnis sollte wie unten sein:
[{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "[email protected]",
"address": {
"street":"201 S 4th St.",
"suite": "Apt. 556",
"city": "Gwenborough",
"zipcode": "92998-3874",
"geo": {
"lat": "-37.3159",
"lng": "81.1496"
}
},
"company": {
"name": "Romaguera-Crona",
"catchPhrase": "Multi-layered client-server neural-net",
"bs": "harness real-time e-markets"
},
"hobbies":[
{
"books":"fiction",
"sports":"football",
},
{
"books":"action",
"sports":"cricket",
},
{
"books":"action",
"sports":"cricket",
},
] },{
"id":2,
"name": "Leanne Graham",
"username": "Bret",
"email": "[email protected]",
"address": {
"street": "Kulas Light",
"suite": "Apt. 556",
"city": "Gwenborough",
"zipcode": "92998-3874",
"geo": {
"lat": "-37.3159",
"lng": "81.1496"
}
},
"phone": ["1-770-736-8031 x56442", "4087917884", "4089088939"],
"website": "hildegard.org",
"dept":[{"name":"divya", "address":"abc"},{"name":"divya1", "address":"abc1"}],
"hobbies":[
{
"books":"fiction",
"sports":"football",
},
{
"books":"action",
"sports":"cricket",
},
{
"books":"action",
"sports":"cricket",
},
]}].
Bitte beachten Sie, dass dies nur ein Beispiel ist. meine Eingabe ist ein Array von JSON und die Schlüssel, die ich behalten wollte. Schlüssel für dieses Beispiel sind "id", "name", "hobbys.books", "hobbys.sports" etc.
Egal was, Sie müssen lernen, auf Array-Elemente und Objekteigenschaften zuzugreifen. Dies führt zu einem grundlegenden Mangel an Verständnis für JavaScript-Grundlagen. – PHPglue