0

I had the following object:

var dataset = [
      [
        {"value":"PRE","formattedValue":"PRE"},
        {"value":"2017-06-15 00:00:00","formattedValue":"15/06/2017 0:00:00"},
        {"value":"COSTA RICA","formattedValue":"COSTA RICA"},
        {"value":"6.15","formattedValue":"6,150"}
      ],
      [
        {"value":"PRE","formattedValue":"PRE"},
        {"value":"2017-06-15 00:00:00","formattedValue":"15/06/2017 0:00:00"},
        {"value":"EL SALVADOR","formattedValue":"EL SALVADOR"}
      ]
]

Its too complex and has data I don't actually need, so I tried to turn it into this:

[
    {
        "estado":       "PRE",
        "fecha":   "2017-06-15 00:00:00",
        "pais":     "COSTA RICA",
        "precio": "6.15",       
    }
]

I finally did it, but I'n unsure why my code works. I done it with this code:

var datafinal = [];
function convertion(){
    var dataobj = dataset.getData();            
    for(var x in dataobj){
        datafinal[x] = { "estado": dataobj[x][0]["value"] };
        datafinal[x]["fecha"] = dataobj[x][1]["value"];
        datafinal[x]["pais"] = dataobj[x][2]["value"];
        datafinal[x]["precio"] = dataobj[x][3]["value"];
    }               
}

If you pay attention, you'll see the first value i add to the new object is using a different format to get added than the rest.

I discovered that if i add every value with the second format it adds nothing. But if i add everything in the first format, it only adds the last value;

So, i made the vale in the first format, and the rest in the second format, and it worked just fine.

...why though, can someone explain to me why is this happening?

4
  • what do you have if you just display dataobj ? Commented Jun 16, 2017 at 21:54
  • You've shown dataset, but not what dataset.getData() returns. Commented Jun 16, 2017 at 21:54
  • dataobj displays the same info the first dataset object has. Commented Jun 16, 2017 at 22:07
  • What’s the confusion? Commented Dec 25, 2024 at 14:32

3 Answers 3

0

This is because at first datafinal[x] does not yet exist, so you need to give it a value, i.e. assign it an object. That is what the first assignment does. The other assignments are mutating the existing value, since you don't want them to replace the value you assigned in the first line.

Notice the other assignments don't assign to datafinal[x] itself, but write to a property of it: so you extend the object that you assigned in the first assignment.

In fact, the first assignment could be broken into two parts:

datafinal[x] = {};
datafinal[x].estado = dataobj[x][0].value;

... and so now all property assignments could look the same (NB: you don't need the square bracket notation for these literal property names). The first assignment is now just initialising the value as an object (as opposed to a number, a string, a boolean...). Without such assignment, you don't have an object, and cannot assign properties to it.

You could maybe make it easier to understand if you would write it in one object literal assignment:

datafinal[x] = { 
    estado: dataobj[x][0].value,
    fecha: dataobj[x][1].value,
    pais: dataobj[x][2].value,
    precio: dataobj[x][3].value
};
Sign up to request clarification or add additional context in comments.

Comments

0

In the current form you are initialling telling it to create an object at that position and 'initialising' it with a value of { "estado": dataobj[x][0]["value"] };. Subsequent attempts to access the object datafinal[x] then access a property of that object and as that property does not yet exist, creates it with your given value. By simple repeating the line datafinal[x] = { "estado": dataobj[x][0]["value"] }; as you suggest with the different keys and values, you are merely overwriting the value of datafinal[x] with a new object each time.

EDIT:

As for why only adding the values in your second format does not work, this is down to the fact that in doing so you are trying to access properties of an object datafinal[x] that does not yet exist. Whereas, by doing one call in your first format creates that object.

3 Comments

Why are we all being mindlessly downvoted? All of our answers allude to identical points that answer the question but have been downvoted.
dont know probably a troll, i cant vote yet dont have enough reputation, but im reading every answer
same for me. What the hell is this guy doing ?
0

If you use your first format it will rewrite all your object everytime you call it

datafinal[x] = { "estado": dataobj[x][0]["value"] };

means that datafinal[x] is equal to that so if you do

datafinal[x] = { "fecha" = dataobj[x][1]["value"] };

it will erase what's in datafinal[x]

that is why your first format doesn't work

Comments

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.