-1

I need to change the origin value to lat/lang coordinate (from another geoJson file) instead of country name:

index = [
  {
    "Name": "Aish Merahrah",
    "Origin": "Egypt",
    "Description": "Made with fenugreek seeds and maize; dough allowed to 
ferment overnight, then flattened and baked."
  },
  {
    "Name": "Ajdov Kruh",
    "Origin": "Slovenia",
    "Description": "Made with buckwheat flour and potato."
  }
]

so the result would be something like:

  {
    "Name": "Ajdov Kruh",
    "Origin": "46.151241, 14.995463",
    "Description": "Made with buckwheat flour and potato."
  }

I'm not sure about the workflow, do I need to extract the JSON value variable and somehow use that variable to obtain lat & lang data? Btw I need to use only JS/JQuery and Node.

pploypiti
  • 59
  • 1
  • 9

3 Answers3

2

Use Array.forEach

let index = [{"Name":"Aish Merahrah","Origin":"Egypt","Description":"Made with fenugreek seeds and maize; dough allowed to ferment overnight, then flattened and baked."},{"Name":"Ajdov Kruh","Origin":"Slovenia","Description":"Made with buckwheat flour and potato."}];
let lat = {"Slovenia": "46.151241, 14.995463", "Egypt": "26.820553, 30.802498"};
index.forEach(o => o.Origin = lat[o.Origin] ?  lat[o.Origin] :  o.Origin); 
console.log(index);
Nikhil Aggarwal
  • 28,197
  • 4
  • 43
  • 59
0

Assuming your geoJson has the following structure (country name as key and lat/long as value):

geolocation = {"Slovenia": "46.151241, 14.995463", "Egypt": "1, 1"}

You can use Array.prototype.map to iterate on index and replacing Origin of each object with the values from geolcoation.

index = [
  {
    "Name": "Aish Merahrah",
    "Origin": "Egypt",
    "Description": "Made with fenugreek seeds and maize; dough allowed to ferment overnight, then flattened and baked."
  },
  {
    "Name": "Ajdov Kruh",
    "Origin": "Slovenia",
    "Description": "Made with buckwheat flour and potato."
  }
]
geolocation = {"Slovenia": "46.151241, 14.995463", "Egypt": "1, 1"};
result = index.map(function(obj) { obj['Origin'] = geolocation[obj['Origin']]; return obj }); 
console.log(result);

You can even use forEach to iterate above and it will still change the index, since changes are done in reference objects.

kiddorails
  • 12,961
  • 2
  • 32
  • 41
0

Just iterate over the object indices and then edit the values with your values.

Assume the given JSON data:


// Creates an Object
let index = [
  {
    "Name": "Aish Merahrah",
    "Origin": "Egypt",
    "Description": "Made with fenugreek seeds and maize; dough allowed to ferment overnight, then flattened and baked."
  },
  {
    "Name": "Ajdov Kruh",
    "Origin": "Slovenia",
    "Description": "Made with buckwheat flour and potato."
  }
]

console.log(`Original Origin 1: ${index[0].Origin}\nOriginal Origin 2: ${index[1].Origin}`);

// Iterate over indices
for(let i=0;i<index.length;i++) {
  // Edit the value
  index[i].Origin = "1234/1234"
}

/*
// Using forEach
index.forEach((elem) => {
  elem.Origin = "Your Value";
});
*/

console.log(`Edited Origin 1: ${index[0].Origin}\nEdited Origin 2: ${index[1].Origin}`);
Alex
  • 2,164
  • 1
  • 9
  • 27