2

I have a JSON response with key-values like:

....
    "usp-custom-90":"45.45257926613316,9.178168599999935"
....

Note usp-custom-90has dash!

I need something like (fields doens't exist, it's just an example):

            data.forEach(({fields})=>{
              coordsB.push(
                ...fields['usp-custom-90']
              );
            });

Where coordsB is an array defined before.

The json would be:

],
      "usp-custom-90":"45.47841306255037,9.120865849999973",
      "_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":[  

I need to push the value of each "usp-custom-90"

Full code (wrong as it is using fields which doesn't exist):

    fetch('https://www.example.it/wp-json/wp/v2/posts?per_page=50&status=publish')
      .then(res => res.json())
      .then(data =>{
        var coordsB = [];
        console.log(data);
        data.forEach(({fields})=>{
          coordsB.push(
            ...fields['usp-custom-90']
          );
        });
10
  • Can you post the real response? Commented Feb 17, 2019 at 23:39
  • @Ele the json there is really, there just would be many usp-custom-90 Commented Feb 17, 2019 at 23:40
  • Please, post the exact response structure. Commented Feb 17, 2019 at 23:41
  • how about something like obj.map(x=> x["usp-custom-90"]), where obj is your json object? Commented Feb 17, 2019 at 23:45
  • 1
    @David784 ok perfect, put that into an answer pls, works Commented Feb 17, 2019 at 23:53

2 Answers 2

2

Based on the data sample linked in the comments, the structure is an array of objects, each object containing a usp-custom-90 property. This is a perfect situation for the map operator for arrays.

So in the above code, this one line will do it all for you. It will create the array and populate it with all the values you're looking for.

var coordsB = data.map(x=> x["usp-custom-90"])
Sign up to request clarification or add additional context in comments.

Comments

0

Something along these lines would do I guess.

Object.entries(object).reduce((ac,[k,v],i,a)=>(ac.push(v['usp-custom-90']),ac),[])

Looks like from your paste bin main object is an array with objects inside which have the key you want then:

YourMainArray.reduce((ac,d,i,a)=>(ac.push(d['usp-custom-90']),ac),[])

Tested it, gives you this:

["45.45257926613316,9.178168599999935", "45.47841306255037,9.120865849999973", "9.924,-84.090", "44.948,9.039", "45.464150416139695,9.1906395499999", "45.651,11.303", "43.83734441524854,7.905822499999999", "45.05926341591318,9.3354875", "44.872988115810074,13.85009094999998", "44.97805886586813,8.895478499999967", "45.472119466144186,9.173527250000006", "45.165,9.183", "41.937,12.441", "45.464993216140186,9.147909499999969", "45.48624411615216,9.16677489999995", "45.209,9.147", "45.464993216140186,9.147909499999969", "41.848264464222716,12.665936949999946", "45.464993216140186,9.147909499999969", "45.46851557705748,9.139416449999999", "44.507,11.314", "36.731,14.873", "36.222,-121.759", "10.093,77.060", "45.454327616134165,9.175796900000023", "45.469282816142574,9.176045000000045"]

3 Comments

how would I put this into my code tho? See updated question with full code
if you pasted an example data with entire 'fields' open I could have said that to you. The object inside Object.entries is your entire object with whatever 'fields' is. Within each MainObject['someKey'] there is an another object with your usp-custom. Isnt this what you want?
Ok test the above

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.