Iam creating a new array from an object.
I do this with a for loop.
for (var key in data["record"])
{
if (data["record"].hasOwnProperty(key))
{
if (data["record"][key]["acquisition.method"] != undefined ) {
newObject[key] = [];
newObject[key]["acquisition.method"] = data["record"][key]["acquisition.method"][0];
if (data["record"][key]["production.date.end"] != undefined ) {
newObject[key]["production.date"] = data["record"][key]["production.date.end"][0];
}
}
}
}
The array looks like this in the console.log:
0: Array[0]
1: Array[0]
2: Array[0]
3: Array[0]
4: Array[0]
5: Array[0]
I want to make it like this:
record: Array[0]
record: Array[0]
record: Array[0]
record: Array[0]
record: Array[0]
How should i do that?
added JSON
newObject = [
"record": [
"something": "value",
"somethingelse": "value"
],
"record": [
"something": "value",
"somethingelse": "value"
],
"record": [
"something": "value",
"somethingelse": "value"
],
"record": [
"something": "value",
"somethingelse": "value"
]
]
Only thing i can manage to make is something like this:
newObject = [
"0": [
"something": "value",
"somethingelse": "value"
],
"1": [
"something": "value",
"somethingelse": "value"
],
"2": [
"something": "value",
"somethingelse": "value"
],
"3": [
"something": "value",
"somethingelse": "value"
]
]
I tried something like this, but i know that push is only for arrays:
var newObject = {};
var records ={};
records["record"] = {};
for (var key in data["record"])
{
if (data["record"].hasOwnProperty(key))
{
if (data["record"][key]["acquisition.method"] != undefined ) {
records["record"]["acquisition.method"] = data["record"][key]["acquisition.method"][0];
if (data["record"][key]["production.date.end"] != undefined ) {
records["record"]["production.date"] = data["record"][key]["production.date.end"][0];
newObject.push(records);
}
}
}
}
What i tried now:
var records =[];
var newObject1 = {};
var newObject2= {};
for (var key in data["record"])
{
if (data["record"].hasOwnProperty(key))
{
if (data["record"][key]["acquisition.method"] != undefined ) {
newObject2["acquisition.method"] = data["record"][key]["acquisition.method"][0];
if (data["record"][key]["production.date.end"] != undefined ) {
newObject2["production.date"] = data["record"][key]["production.date.end"][0];
newObject1["record"] = newObject2;
}
}
}
records.push(newObject1);
}
If I am understanding your code correctly (and I would like to think that I am 🙂 ), what you are trying to do is make it so that you can access each of the records in your array with a string (key). This is made possible by using JSON. Even with JSON, you should still be able to access the elements in the array by their index (the integer keys that you are seeing).
UPDATE
Think about it like this, you are trying to access a bunch of objects in an array with a single key. The problem is that a key, by definition, refers to a single value. Thus, you cannot use the same key for multiple objects. You can, however, create an array that the key, “record”, refers to which will return all of those inner key-value pairs.
UPDATE 2
Think of it like this: imagine that you are using your original array (the one made in the code you originally posted). Now imagine that you set that array to be the value of a variable named
var rec. When viewing the value of the variablerecin the console, you will see the exact output that you listed on your original post (“0: Array[0], 1: Array[1]…..”). Taking a step back, we know that the variablerecrefers to (i.e., it is an array containing) all of these arrays (as it should be). Now, here is what you do: you create a new object,obj1, and give it a property namedrecordand set the value of that property to be your variablerec. This means that you have an object,obj1, that can access your data viaobj["record"].Note that this is a conceptual solution – not a full solution.
Let me know if you have any questions. Good luck! 🙂