如何获取json中的特定对象并将其值推入数组?

时间:2019-02-17 20:01:33

标签: javascript jquery

我运行这个

[  
   {  
      "id":128402,
      "date":"2019-02-16T17:55:05",
      "date_gmt":"2019-02-16T17:55:05",
      "guid":{  
         "rendered":"https:\/\/www.example.it\/bob-marley-a-san-siro\/"
      },
      "modified":"2019-02-16T17:55:05",
      "modified_gmt":"2019-02-16T17:55:05",
      "slug":"bob-marley-a-san-siro",
      "status":"publish",
      "type":"post",
      "link":"https:\/\/www.example.it\/bob-marley-a-san-siro\/",
      "title":{  
         "rendered":"Bob Marley a San Siro"
      },
      "content":{  
         "rendered":"<p>dalla bolgia e dirigerci verso le pi\u00f9 sicure gradinate del secondo anello, ancora parzialmente libere. &nbsp;<br \/>Beh, il resto \u00e8 storia.&nbsp;<br \/>&nbsp;<\/p>\n",
         "protected":false
      },
      "excerpt":{  
         "rendered":"<p class=\"card-text dark\">La mia quarta tappa del percorso scolastico volgeva ormai al termine.&nbsp; Di [&hellip;]<\/p>\n",
         "protected":false
      },
      "author":37,
      "featured_media":128403,
      "comment_status":"closed",
      "ping_status":"open",
      "sticky":false,
      "template":"",
      "format":"standard",
      "meta":[  

      ],
      "categories":[  
         137,
         667
      ],
      "tags":[  
         738,
         739,
         740,
         741
      ],
      "fields":{  
         "usp-custom-19":[  
            "45.47841306255037"
         ],
         "usp-custom-20":[  
            "9.120865849999973"
         ],
         "usp-custom-21":[  
            "Piazzale Angelo Moratti"
         ],
         "usp-custom-60":[  
            "Piazzale Angelo Moratti, 20151 Milano MI, Italia"
         ],
         "usp-custom-90":[  
            "45.47841306255037,9.120865849999973"
         ],
         "usp-custom-14":[  
            "1980"
         ],
         "usp-custom-16":[  
            "27"
         ],
         "usp-custom-17":[  
            "6"
         ],
         "usp-custom-80":[  
            "27-6-1980"
         ],
         "usp-author-id":[  
            "37"
         ],
         "is_submission":[  
            "1"
         ],
         "usp-post-id":[  
            "128402"
         ],
         "usp-post-time":[  
            "sabato, Febbraio 16, 2019 @ 05:55:05 pm"
         ],
         "usp-form-id":[  
            "11"
         ],
         "_thumbnail_id":[  
            "128403"
         ],
         "usp-file-single":[  
            "https:\/\/www.example.it\/wp-content\/uploads\/2019\/02\/2019-02-16_5c684e79f1252_biglietto.concerto-sansiro-bob-marley.png"
         ],
         "_edit_lock":[  
            "1550432185:1"
         ]
      },
      "_links":{  
         "self":[  
            {  
               "href":"https:\/\/www.example.it\/wp-json\/wp\/v2\/posts\/128402"
            }
         ],
         "collection":[  
            {  
               "href":"https:\/\/www.example.it\/wp-json\/wp\/v2\/posts"
            }
         ],
         "about":[  
            {  
               "href":"https:\/\/www.example.it\/wp-json\/wp\/v2\/types\/post"
            }
         ],
         "author":[  
            {  
               "embeddable":true,
               "href":"https:\/\/www.example.it\/wp-json\/wp\/v2\/users\/37"
            }
         ],
         "replies":[  
            {  
               "embeddable":true,
               "href":"https:\/\/www.example.it\/wp-json\/wp\/v2\/comments?post=128402"
            }
         ],
         "version-history":[  
            {  
               "count":0,
               "href":"https:\/\/www.example.it\/wp-json\/wp\/v2\/posts\/128402\/revisions"
            }
         ],
         "wp:featuredmedia":[  
            {  
               "embeddable":true,
               "href":"https:\/\/www.example.it\/wp-json\/wp\/v2\/media\/128403"
            }
         ],
         "wp:attachment":[  
            {  
               "href":"https:\/\/www.example.it\/wp-json\/wp\/v2\/media?parent=128402"
            }
         ],
         "wp:term":[  
            {  
               "taxonomy":"category",
               "embeddable":true,
               "href":"https:\/\/www.example.it\/wp-json\/wp\/v2\/categories?post=128402"
            },
            {  
               "taxonomy":"post_tag",
               "embeddable":true,
               "href":"https:\/\/www.example.it\/wp-json\/wp\/v2\/tags?post=128402"
            }
         ],
         "curies":[  
            {  
               "name":"wp",
               "href":"https:\/\/api.w.org\/{rel}",
               "templated":true
            }
         ]
      }
   }
]

我需要获取usp-custom-90的值

json可能有很多,所以我需要把它们全部都拿出来并将它们推入一个数组中,我尝试过:

let op = data.map( ({ ['usp-custom-90']: uspCustom90 }) => uspCustom90 )

但这给了我[undefined]

我需要在名为usp-custom-90的数组中找到并推送coords[]中的每个值

3 个答案:

答案 0 :(得分:2)

您需要更改

let op = data.map( ({ ['usp-custom-90']: uspCustom90 }) => uspCustom90 )

对此

let op = data.map( ({ fields }) => fields["usp-custom-90"] )

演示

const data = [{"id":128402,"date":"2019-02-16T17:55:05","date_gmt":"2019-02-16T17:55:05","guid":{"rendered":"https:\/\/www.example.it\/bob-marley-a-san-siro\/"},"modified":"2019-02-16T17:55:05","modified_gmt":"2019-02-16T17:55:05","slug":"bob-marley-a-san-siro","status":"publish","type":"post","link":"https:\/\/www.example.it\/bob-marley-a-san-siro\/","title":{"rendered":"Bob Marley a San Siro"},"content":{"rendered":"<p>dalla bolgia e dirigerci verso le pi\u00f9 sicure gradinate del secondo anello, ancora parzialmente libere. &nbsp;<br \/>Beh, il resto \u00e8 storia.&nbsp;<br \/>&nbsp;<\/p>\n","protected":!1},"excerpt":{"rendered":"<p class=\"card-text dark\">La mia quarta tappa del percorso scolastico volgeva ormai al termine.&nbsp; Di [&hellip;]<\/p>\n","protected":!1},"author":37,"featured_media":128403,"comment_status":"closed","ping_status":"open","sticky":!1,"template":"","format":"standard","meta":[],"categories":[137,667],"tags":[738,739,740,741],"fields":{"usp-custom-19":["45.47841306255037"],"usp-custom-20":["9.120865849999973"],"usp-custom-21":["Piazzale Angelo Moratti"],"usp-custom-60":["Piazzale Angelo Moratti, 20151 Milano MI, Italia"],"usp-custom-90":["45.47841306255037,9.120865849999973"],"usp-custom-14":["1980"],"usp-custom-16":["27"],"usp-custom-17":["6"],"usp-custom-80":["27-6-1980"],"usp-author-id":["37"],"is_submission":["1"],"usp-post-id":["128402"],"usp-post-time":["sabato, Febbraio 16, 2019 @ 05:55:05 pm"],"usp-form-id":["11"],"_thumbnail_id":["128403"],"usp-file-single":["https:\/\/www.example.it\/wp-content\/uploads\/2019\/02\/2019-02-16_5c684e79f1252_biglietto.concerto-sansiro-bob-marley.png"],"_edit_lock":["1550432185:1"]},"_links":{"self":[{"href":"https:\/\/www.example.it\/wp-json\/wp\/v2\/posts\/128402"}],"collection":[{"href":"https:\/\/www.example.it\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.example.it\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":!0,"href":"https:\/\/www.example.it\/wp-json\/wp\/v2\/users\/37"}],"replies":[{"embeddable":!0,"href":"https:\/\/www.example.it\/wp-json\/wp\/v2\/comments?post=128402"}],"version-history":[{"count":0,"href":"https:\/\/www.example.it\/wp-json\/wp\/v2\/posts\/128402\/revisions"}],"wp:featuredmedia":[{"embeddable":!0,"href":"https:\/\/www.example.it\/wp-json\/wp\/v2\/media\/128403"}],"wp:attachment":[{"href":"https:\/\/www.example.it\/wp-json\/wp\/v2\/media?parent=128402"}],"wp:term":[{"taxonomy":"category","embeddable":!0,"href":"https:\/\/www.example.it\/wp-json\/wp\/v2\/categories?post=128402"},{"taxonomy":"post_tag","embeddable":!0,"href":"https:\/\/www.example.it\/wp-json\/wp\/v2\/tags?post=128402"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":!0}]}}];

let op = data.map( ({ fields }) => fields["usp-custom-90"] )

console.log(op)

答案 1 :(得分:2)

使用Array#reduce,您可以执行以下操作。

const data = [{"id":128402,"date":"2019-02-16T17:55:05","date_gmt":"2019-02-16T17:55:05","guid":{"rendered":"https:\/\/www.example.it\/bob-marley-a-san-siro\/"},"modified":"2019-02-16T17:55:05","modified_gmt":"2019-02-16T17:55:05","slug":"bob-marley-a-san-siro","status":"publish","type":"post","link":"https:\/\/www.example.it\/bob-marley-a-san-siro\/","title":{"rendered":"Bob Marley a San Siro"},"content":{"rendered":"<p>dalla bolgia e dirigerci verso le pi\u00f9 sicure gradinate del secondo anello, ancora parzialmente libere. &nbsp;<br \/>Beh, il resto \u00e8 storia.&nbsp;<br \/>&nbsp;<\/p>\n","protected":!1},"excerpt":{"rendered":"<p class=\"card-text dark\">La mia quarta tappa del percorso scolastico volgeva ormai al termine.&nbsp; Di [&hellip;]<\/p>\n","protected":!1},"author":37,"featured_media":128403,"comment_status":"closed","ping_status":"open","sticky":!1,"template":"","format":"standard","meta":[],"categories":[137,667],"tags":[738,739,740,741],"fields":{"usp-custom-19":["45.47841306255037"],"usp-custom-20":["9.120865849999973"],"usp-custom-21":["Piazzale Angelo Moratti"],"usp-custom-60":["Piazzale Angelo Moratti, 20151 Milano MI, Italia"],"usp-custom-90":["45.47841306255037,9.120865849999973"],"usp-custom-14":["1980"],"usp-custom-16":["27"],"usp-custom-17":["6"],"usp-custom-80":["27-6-1980"],"usp-author-id":["37"],"is_submission":["1"],"usp-post-id":["128402"],"usp-post-time":["sabato, Febbraio 16, 2019 @ 05:55:05 pm"],"usp-form-id":["11"],"_thumbnail_id":["128403"],"usp-file-single":["https:\/\/www.example.it\/wp-content\/uploads\/2019\/02\/2019-02-16_5c684e79f1252_biglietto.concerto-sansiro-bob-marley.png"],"_edit_lock":["1550432185:1"]},"_links":{"self":[{"href":"https:\/\/www.example.it\/wp-json\/wp\/v2\/posts\/128402"}],"collection":[{"href":"https:\/\/www.example.it\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.example.it\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":!0,"href":"https:\/\/www.example.it\/wp-json\/wp\/v2\/users\/37"}],"replies":[{"embeddable":!0,"href":"https:\/\/www.example.it\/wp-json\/wp\/v2\/comments?post=128402"}],"version-history":[{"count":0,"href":"https:\/\/www.example.it\/wp-json\/wp\/v2\/posts\/128402\/revisions"}],"wp:featuredmedia":[{"embeddable":!0,"href":"https:\/\/www.example.it\/wp-json\/wp\/v2\/media\/128403"}],"wp:attachment":[{"href":"https:\/\/www.example.it\/wp-json\/wp\/v2\/media?parent=128402"}],"wp:term":[{"taxonomy":"category","embeddable":!0,"href":"https:\/\/www.example.it\/wp-json\/wp\/v2\/categories?post=128402"},{"taxonomy":"post_tag","embeddable":!0,"href":"https:\/\/www.example.it\/wp-json\/wp\/v2\/tags?post=128402"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":!0}]}}];

const coords = data.reduce((a, {fields})=>{
  if(fields['usp-custom-90']) a.push(fields['usp-custom-90']);
  return a;
}, []);

console.log(coords);

如果它是来自URL的json数据,那么...

fetch('/some/url')
.then(res=>res.json())
.then(data=>{
  const coords = data.reduce((a, {fields})=>{
    if(fields['usp-custom-90']) a.push(fields['usp-custom-90']);
    return a;
  }, []);

  console.log(coords);
});

答案 2 :(得分:0)

你不能做

let data = [  
       {  
          "id":128402,
          "date":"2019-02-16T17:55:05",
          "date_gmt":"2019-02-16T17:55:05",
          "guid":{  
             "rendered":"https:\/\/www.example.it\/bob-marley-a-san-siro\/"
          },
          "modified":"2019-02-16T17:55:05",
          "modified_gmt":"2019-02-16T17:55:05",
          "slug":"bob-marley-a-san-siro",
          "status":"publish",
          "type":"post",
          "link":"https:\/\/www.example.it\/bob-marley-a-san-siro\/",
          "title":{  
             "rendered":"Bob Marley a San Siro"
          },
          "content":{  
             "rendered":"<p>dalla bolgia e dirigerci verso le pi\u00f9 sicure gradinate del secondo anello, ancora parzialmente libere. &nbsp;<br \/>Beh, il resto \u00e8 storia.&nbsp;<br \/>&nbsp;<\/p>\n",
             "protected":false
          },
          "excerpt":{  
             "rendered":"<p class=\"card-text dark\">La mia quarta tappa del percorso scolastico volgeva ormai al termine.&nbsp; Di [&hellip;]<\/p>\n",
             "protected":false
          },
          "author":37,
          "featured_media":128403,
          "comment_status":"closed",
          "ping_status":"open",
          "sticky":false,
          "template":"",
          "format":"standard",
          "meta":[  
    
          ],
          "categories":[  
             137,
             667
          ],
          "tags":[  
             738,
             739,
             740,
             741
          ],
          "fields":{  
             "usp-custom-19":[  
                "45.47841306255037"
             ],
             "usp-custom-20":[  
                "9.120865849999973"
             ],
             "usp-custom-21":[  
                "Piazzale Angelo Moratti"
             ],
             "usp-custom-60":[  
                "Piazzale Angelo Moratti, 20151 Milano MI, Italia"
             ],
             "usp-custom-90":[  
                "45.47841306255037,9.120865849999973"
             ],
             "usp-custom-14":[  
                "1980"
             ],
             "usp-custom-16":[  
                "27"
             ],
             "usp-custom-17":[  
                "6"
             ],
             "usp-custom-80":[  
                "27-6-1980"
             ],
             "usp-author-id":[  
                "37"
             ],
             "is_submission":[  
                "1"
             ],
             "usp-post-id":[  
                "128402"
             ],
             "usp-post-time":[  
                "sabato, Febbraio 16, 2019 @ 05:55:05 pm"
             ],
             "usp-form-id":[  
                "11"
             ],
             "_thumbnail_id":[  
                "128403"
             ],
             "usp-file-single":[  
                "https:\/\/www.example.it\/wp-content\/uploads\/2019\/02\/2019-02-16_5c684e79f1252_biglietto.concerto-sansiro-bob-marley.png"
             ],
             "_edit_lock":[  
                "1550432185:1"
             ]
          },
          "_links":{  
             "self":[  
                {  
                   "href":"https:\/\/www.example.it\/wp-json\/wp\/v2\/posts\/128402"
                }
             ],
             "collection":[  
                {  
                   "href":"https:\/\/www.example.it\/wp-json\/wp\/v2\/posts"
                }
             ],
             "about":[  
                {  
                   "href":"https:\/\/www.example.it\/wp-json\/wp\/v2\/types\/post"
                }
             ],
             "author":[  
                {  
                   "embeddable":true,
                   "href":"https:\/\/www.example.it\/wp-json\/wp\/v2\/users\/37"
                }
             ],
             "replies":[  
                {  
                   "embeddable":true,
                   "href":"https:\/\/www.example.it\/wp-json\/wp\/v2\/comments?post=128402"
                }
             ],
             "version-history":[  
                {  
                   "count":0,
                   "href":"https:\/\/www.example.it\/wp-json\/wp\/v2\/posts\/128402\/revisions"
                }
             ],
             "wp:featuredmedia":[  
                {  
                   "embeddable":true,
                   "href":"https:\/\/www.example.it\/wp-json\/wp\/v2\/media\/128403"
                }
             ],
             "wp:attachment":[  
                {  
                   "href":"https:\/\/www.example.it\/wp-json\/wp\/v2\/media?parent=128402"
                }
             ],
             "wp:term":[  
                {  
                   "taxonomy":"category",
                   "embeddable":true,
                   "href":"https:\/\/www.example.it\/wp-json\/wp\/v2\/categories?post=128402"
                },
                {  
                   "taxonomy":"post_tag",
                   "embeddable":true,
                   "href":"https:\/\/www.example.it\/wp-json\/wp\/v2\/tags?post=128402"
                }
             ],
             "curies":[  
                {  
                   "name":"wp",
                   "href":"https:\/\/api.w.org\/{rel}",
                   "templated":true
                }
             ]
          }
       }
    ]
    
    let op = data[0].fields["usp-custom-90"].map(e => e);

    console.log(op);