Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9222517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:51:02+00:00 2026-06-18T03:51:02+00:00

Iam creating a new array from an object. I do this with a for

  • 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);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T03:51:03+00:00Added an answer on June 18, 2026 at 3:51 am

    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! 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating a string array with values from a custom object, like this:
I am creating my cells from an array. The last object in my area
when i am creating new android project from visual studio 2010 it gives the
Possible Duplicate: Java 1.6: Creating an array of List<T> How can I initialize this
I am creating a new xml document from scratch using a google defined format
I am creating a form like this: $model = new RequestForm; $form = new
I am creating my own object: gridObject = new Object(); I am then using
I'm new to PHP and I am reading data from an excel spreadsheet and
I am creating a SQLite database. To get data from the table I am
Here is how i am creating the zend db connection $DB = new Zend_Db_Adapter_Pdo_Mysql(array(host

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.