0

I have a json file array with data and I want to use it in a jquery array.

My json file is like this;

[{"id":"1","value":"0.00","score":"a"},
{"id":"2","value":"0.00","score":"c"},
{"id":"3","value":"12.56","score":"e"}]

and then my jquery array outcome i want should be like this (but missing the score and value data.

var setup = [
{
    "type": "single",
    "subtype": {
        "type": "Point",
        "score": use 'score' of the json file
    },
    "properties": {
        "text": use 'value' of the json file,
        "height": 60
    }
},
{
    "type": "single",
    "subtype": {
        "type": "Point",
        "score": use score of the json file
    },
    "properties": {
        "text": use value of the json file,
        "height": 60
    }
}
];

Below I made an attemp to set up a jquery array but i am mnot so good in this. I think i have to use a double $each? but how do I make the array?

var setup = [];
    // FETCHING DATA FROM JSON FILE 
  $.getJSON("results.json", function(data) { 

        // ITERATING THROUGH OBJECTS 
        $.each(data, function (key, value) { 
               var test = "";
                     $.each(value, function(index, obj) {
                        });
         
        }); 


   setup.push(setup);    

          
    }); 

Thanks in advance :)

2 Answers 2

2

You could use Array.prototype.map() method to get the result.

const data = [
  { id: '1', value: '0.00', score: 'a' },
  { id: '2', value: '0.00', score: 'c' },
  { id: '3', value: '12.56', score: 'e' },
];

const ret = data.map((x) => ({
  type: 'single',
  subtype: {
    type: 'Point',
    score: x.score,
  },
  properties: {
    text: x.value,
    height: 60,
  },
}));
console.log(ret);

Sign up to request clarification or add additional context in comments.

1 Comment

works like a charm :) thank you i put in ajax--> $.ajax({ method: "GET", dataType: "json", url: 'https:bla.json', data: data, success: function(data) { const ret = data.map((x) => ({ type: 'single', geometry: { type: 'Point', score: [x.score1, x.score2], }, properties: { text: x.value, height: 30, } })); console.log(ret); // do some other stuff } });
1

You can use one each loop and pass the object with the elements you need. Check the below example:

var jsonData = [{
    "id": "1",
    "value": "0.00",
    "score": "a"
  },
  {
    "id": "2",
    "value": "0.00",
    "score": "c"
  },
  {
    "id": "3",
    "value": "12.56",
    "score": "e"
  }
]

var setup = [];
// FETCHING DATA FROM JSON FILE 
$.each(jsonData, function(key, value) {
  var test = "";
  d = {
    "type": "single",
    "subtype": {
      "type": "Point",
      "score": value.score
    },
    "properties": {
      "text": value.value,
      "height": 60
    }
  };
  
  setup.push(d)
});

console.log(setup)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

1 Comment

Thank you, this method has also worked for me :)

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.