Array of Objects to Objects with Modifications

javascript update object property in array
javascript update object in array es6
find and update object in array javascript
change value of key in object javascript
javascript array of objects
javascript manipulate array of objects
add property to array of objects typescript
change object value javascript
Hi,

Question: How could I transform this:

 console.log(prepararCoordenadas([
    { "nome": "Salvador",       "coordenadas": ["-23.6821604","-46.8754915"]},
    { "nome": "Brasília",  "coordenadas": ["-22.9137531","-73.5860657"]},
    { "nome": "Recife",          "coordenadas": [ "-8.0462741","-35.0000824"]}
  ]));

on this ?

{ 
  '0':
   { nome: 'Salvador',
     coordenadas: [-46.8755, -23.6822 ] },
  '1':
   { nome: 'Brasília',
     coordenadas: [-73.5861, -22.9137] },
  '2': { nome: 'Recife', 
          coordenadas: [-35.0001, -8.0463] }
}
Diff between the two results.

First - is a array of object - Have the name of the city and a array of "coordenadas" like string.

Second - is a complete object - Have the name of the city (same the first one) and a array of "coordenadas" like decimal with 4 numbers after dot; - Latitude and longitude are changed - position - first longitude;

Language: Javascript

I tried to use the ** Object.assign ** but lacked the changes with the "coordenadas".

function prepararCoordenadas(array){

    let obj = Object.assign({},array);

    return obj;
  }

  // Test
  console.log(prepararCoordenadas([
    { "nome": "Salvador",       "coordenadas": ["-23.6821604","-46.8754915"]},
    { "nome": "Brasília",  "coordenadas": ["-22.9137531","-73.5860657"]},
    { "nome": "Recife",          "coordenadas": [ "-8.0462741","-35.0000824"]}
  ]));

Could any one help with this puzzle function ?

You could reduce into an object and transform the coordenadas with each iteration:

const strToFixed4 = str => Number(str).toFixed(4);
const prepararCoordenadas = (array) => array.reduce(
  (accum, { nome, coordenadas: [lat, lng] }, i) => (
    {
      ...accum,
      [i]: { nome, coordenadas: [strToFixed4(lng), strToFixed4(lat)] }
    }),
  {}
);

console.log(prepararCoordenadas([
  { "nome": "Salvador",       "coordenadas": ["-23.6821604","-46.8754915"]},
  { "nome": "Brasília",  "coordenadas": ["-22.9137531","-73.5860657"]},
  { "nome": "Recife",          "coordenadas": [ "-8.0462741","-35.0000824"]}
]));

JavaScript - Difference between Array and Array-like object, are a special type of variable that is also mutable and can also be used to store a list of values. Numbers, booleans and strings are the primitive datatypes in JavaScript. JavaScript also allows us to compose these datatypes into compound datatypes, such as arrays and objects. Both array and objects are ways of grouping pieces of data together into meaningful collections.

function prepararCoordenadas(array){
    let objs = {...array}; //es6 spread operator
    for (index in objs) { //for each object in objs
        objs[index].coordenadas.forEach(coordenada => Number(coordenada)); //for each coordenada in coordenadas array, convert type to number
    }
    return objs;
  }

The es6 spread operator in this case practically does the same thing as object.assign(), however, it has a far more simpler syntax.

I think creates the desired result, if you have any questions please feel free to ask.

Objects vs. Arrays - DEV Community ‍ ‍ , Objects are an integral and foundational aspect of most JavaScript programs. consist of an array of many objects containing all the pertinent information for Using the same method, an object's property can be modified by  For information on creating rectangular, polar, and path array objects, refer to the online Help system. Editing solids in AutoCAD. You can edit 3D solids in a variety of different ways that you can’t edit other objects. You can use grip editing to change the shape of 3D solids, or use Boolean operations on a 3D solid to create complex models.

The final answer Guys! Thank U Two!

const prepararCoordenadas = (array) => array.reduce(
  (accum, { nome, coordenadas: [lat, lng] }, i) => (
    {
      ...accum,
      [i]: { nome, coordenadas: [Number(parseFloat(lng).toFixed(4)), Number(parseFloat(lat).toFixed(4))] }
    }),
  {}
);

console.log(prepararCoordenadas([
  { "nome": "São Paulo",       "coordenadas": ["-23.6821604","-46.8754915"]},
  { "nome": "Rio de Janeiro",  "coordenadas": ["-22.9137531","-73.5860657"]},
  { "nome": "Recife",          "coordenadas": [ "-8.0462741","-35.0000824"]}
]));

How To Create, Modify, and Loop through Objects in JavaScript , If you want to make changes only in the cloned array of objects and not in the original one, there is a way to do it. The basic idea is to create a new empty object for  Array Objects. Demonstrates the syntax for creating an array of custom objects. int unit = 40; int count; Module [] mods; void setup () { size (640, 360); noStroke (); int wideCount = width / unit; int highCount = height / unit; count = wideCount * highCount; mods = new Module [count]; int index = 0; for (int y = 0; y < highCount;

Immutability in Array of objects, using Map method, Arrays , functions , even numbers ! Because of this, you can do some really interesting things, such as modifying the prototypes of Objects, Arrays, etc. //  Student st [5]; - We created an array of 5 objects of the Student class where each object represents a student having a name and marks. The first for loop is for taking the input of name and marks of the students. getName () and getMarks () are the functions to take the input of name and marks respectively.

Advanced Javascript: Objects, Arrays, and Array-Like objects, Javascript Objects and Arrays are mutable, which means their state can be modified after it is being created. Objects and arrays are being  In this beginners object oriented java programming video tutorial you will learn about array of objects in detail with example. This video lecture teaches how to create array of objects, how to

Cloning an Array of Objects in JavaScript, We will represent the journal that Jacques keeps as an array of objects. let journal = [ {events: We saw that object values can be modified. The types of values  To add another item to an array, just type a comma, followed by another value. I'll add two more objects here. The name will be table, inventory ten and a unit price of 123.75. The last product is a sofa. There’s two in stock and the unit price is 399.50, okay. We have our array in place with three different objects or three products in it.

Comments
  • ThankU! Only one thing, how can I interchange the "cordenadas" ? Longitude to latitude position.