0

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);
}
3
  • 1
    An array always has integers as keys. And keys need to be unique, so your expected results seems impossible to me, even if you used an object. Could you use JSON please to specify what exactly you want? Commented Jan 29, 2013 at 19:13
  • How should i build up my JSON file? Commented Jan 29, 2013 at 19:21
  • 1
    That is invalid JSON. Neither have arrays keys, nor can objects have repeated keys. Commented Jan 29, 2013 at 19:28

1 Answer 1

1

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 variable rec in 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 variable rec refers 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 named record and set the value of that property to be your variable rec. This means that you have an object, obj1, that can access your data via obj["record"].

Note that this is a conceptual solution - not a full solution.


Let me know if you have any questions. Good luck! :)

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

7 Comments

Yes you are right :) How should i build up my JSON file so that the new JSON object is the same as the example i have given?
JSON won't help. The OP is handling plain JS objects
Now that I see what he tried to do with JSON, I think I have a better understanding of what he was asking.
i would like to thank you for helping me. I'm a bit desperate to solve this problem. I updated my question and tried to solve the problem with your information, but still no luck :(
Thank you for the effort to helping me. I tried to follow your hints and tried to get to an solution. But i still not there. You can see what i tried in my anser. The last bit of code.
|

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.