-1

I am in the process of figuring out how to take an object that is shaped like this:

{
    "stores": {
        store: [
            {
                location: {
                     city: "Austin,
                     state: "TX"
                }
            }
        ],
        [ ... 100 more stores ]
    }
}

What I need to do is create a new object where the store would be the on the top level so that I can shape that as I need to rather than shaping the stores.

{
    "store": [
        {
            city: "Austin",
            state: "TX"

        },
        {
            city: "San Francisco",
            state: "CA"
        },
        { ... 100 more }
    ]
}

What would be the best way to have a new object created from the 1st object and be able to take the 2nd level and make it the first level? Do I have to create a new object or can I just manipulate the 1st?

How can this be done with the littlest amount of code needed without making it hard to maintain?

Why would the approach be used to shape the new object?

Thank you for any help in understanding the process of tackling this task

My final output needs to look similar to this:

{
    company: "ABC",
    companyId: 1234,
    nasdaq: ABCND,
    store: [], // this is where I will have all the objects stores in array
    employees: 1000000
}

My hopes are that this will give more clarity to what I am trying to do with taking the first and shaping it to look like second.

Attention

After having discussions in the comments, this question should be closed because of the lack of clarity in the question and how it can be confusing. My hopes are that it is not closed, rather used as a reference for how not to ask a question on SO.

pertrai1
  • 4,146
  • 11
  • 46
  • 71
  • 1
    Add actual data and your code. – Tushar Jun 23 '17 at 06:02
  • That is actual data. Is it not in the right form? That is how the response is returned that I need to shape differently. The only process I know of is to use `Object.create()`, but even with that I am not good enough to know how to do that. – pertrai1 Jun 23 '17 at 06:04
  • 4
    Are you sure you have valid object in `"stores": [ store: [...]]`? – Fabio Jun 23 '17 at 06:10
  • 2
    Are you sure you've given a correct format? Because you have an array `stores` which has `store` as a key. – A. L Jun 23 '17 at 06:11
  • Sorry fixed the bad copy of the code – pertrai1 Jun 23 '17 at 06:14
  • 2
    `let stores = storeObject.stores.store;` will give you collection of all `store` objects – Fabio Jun 23 '17 at 06:15
  • @Fabio so that will create a new object for me? If so, can you explain a bit more? – pertrai1 Jun 23 '17 at 06:16
  • It will create new reference to already existed object – Fabio Jun 23 '17 at 06:16
  • So how are you referencing the rest of the stores? `[ ... 100 more stores ]` doesn't help because your `store` is an array of objects that contains only a single object. Unless you put that in the wrong place again.. – A. L Jun 23 '17 at 06:17
  • @A.Lau I did not want to have to add all of the properties for each store, but the bottom line is that the array has many objects inside of the array and I need that store array to be on the top level. – pertrai1 Jun 23 '17 at 06:18
  • `[ ... 100 more stores ]` is placed outside of the `store` key. By your second example. I'm guessing it's supposed to be part of the `store` key – A. L Jun 23 '17 at 06:25
  • Possible duplicate of [Fastest way to flatten / un-flatten nested JSON objects](https://stackoverflow.com/questions/19098797/fastest-way-to-flatten-un-flatten-nested-json-objects) – autistic Jun 23 '17 at 06:27
  • @Seb That can't be close to the easiest way to handle this question. Are you saying that all of that code is needed to take one object and shape it differently? – pertrai1 Jun 23 '17 at 06:30
  • @pertrai1 You never asked for the *easiest* way. You asked for the *best* way (which is a vague word to use; I assumed *fast*), using code which *you don't have to maintain* (because that code is already tested). Nonetheless, this question you've asked has *other* close reasons... – autistic Jun 23 '17 at 06:37
  • **unclear what you're asking** Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the [How to Ask](https://stackoverflow.com/help/how-to-ask) page for help clarifying this question. – autistic Jun 23 '17 at 06:40
  • **too broad** Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the [How to Ask](https://stackoverflow.com/help/how-to-ask) page for help clarifying this question. – autistic Jun 23 '17 at 06:40
  • **primarily opinion-based** Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. – autistic Jun 23 '17 at 06:40
  • @Seb so you are saying that in order for me to clone an object to shape it another way I would have to take the given approach that has been shown in that answer? If that is the case I can certainly go that way, but I am not advanced enough to know if that is true or not. If true, we can close this and I can try to make heads/tails of that question – pertrai1 Jun 23 '17 at 06:40
  • @pertrai1 No. In general, I'm suggesting that you **think** about your questions *before you ask them*! I was going to post an answer to all of your questions, until it seemed to boil down to *there are multiple questions here* and *they're all answered elsewhere*! – autistic Jun 23 '17 at 06:44
  • @Seb sorry, totally disagree with your comments. This is an open community where I should be able to ask the question to the best of my abilities without having to go through a bunch of guidelines. It is up to you and others if this should be closed, but the way that I asked it is the way I know how to – pertrai1 Jun 23 '17 at 06:44
  • @Seb there is one question here and that is how to take one object and shape it in a different way. What other questions do you see that make this confusing? – pertrai1 Jun 23 '17 at 06:46
  • @pertrai1 Am I expected to assume you're capable of counting *question marks*? In addition to that, you need to eliminate the [double-barreled question](https://en.wikipedia.org/wiki/Double-barreled_question). – autistic Jun 23 '17 at 06:49
  • @Seb have you heard the saying about what happens when you assume? Close the question if you feel that compelled that this is not something that I have not asked correctly. I will go about learning about double barreled questions before ever posting again here on SO – pertrai1 Jun 23 '17 at 06:55
  • @Fabio please add that as an answer so I can accept that please – pertrai1 Jun 23 '17 at 06:57
  • @pertrai1 There are at least three questions there; four if you split up the double-barreled question. Rather than clarifying your intended question (as prompted, perhaps by eliminating the others), you've just added more off-topic details... Do you know that people sometimes write their own self-fulfilling prophecies? – autistic Jun 23 '17 at 07:18
  • @Seb I added vote to close – pertrai1 Jun 23 '17 at 07:27

3 Answers3

0

object.stores will give you the second-level object.

You don't need to "create a new object", or "create a different object structure", or "manipulate an object", or "make the second level the first level"; you just need to refer to the sub-object you want, as in

{
    company: "ABC",
    companyId: 1234,
    nasdaq: ABCND,
    store: object.stores, // this is where I will have all the objects stores in array
    employees: 1000000
}
  • So I don't have to create a new object, rather just a reference to that second level? – pertrai1 Jun 23 '17 at 06:16
  • @pertrai1 yes, but know that if you edit the reference, it will also edit the original. So it depends on if you mind mutating the original object – A. L Jun 23 '17 at 06:17
  • he needs a store array not multiple store arrays from his question we will have multiple store arrays – Rahul Singh Jun 23 '17 at 06:18
  • @RahulSingh that is correct, just a store array that has many objects inside of it. Just one store that has many objects that have properties and I only added a few just so I did not take up room in the post – pertrai1 Jun 23 '17 at 06:21
  • @pertrai1 can you add ore ore store in the stores it will be much clearer, as we cannot have duplicate keys – Rahul Singh Jun 23 '17 at 06:24
  • @RahulSingh Good catch. Sorry I did not mean to have that second key in there, rather just another object. – pertrai1 Jun 23 '17 at 06:35
0

Assuming, you have a structure like

{
    stores: {
        store1: [{ location: { city: "Austin", state: "TX" } }],
        store2: [{ location: { /* ... */ } }],
    }
}

you could just concat all inner objects with the array.

var object = { stores: { store1: [{ location: { city: "Austin", state: "TX" } }], store2: [{ location: { city: "Austin", state: "TX" } }, { location: { city: "San Francisco", state: "CA" } }] } },
    flat = {
        stores: Object.keys(object.stores).reduce(function (r, k) {
            return r.concat(object.stores[k]);
        }, [])
    };

console.log(flat);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • This is a helpful start for me. Could you look at the changed revision as I accidently had a multiple key in the first object. There is only 1 location. – pertrai1 Jun 23 '17 at 06:48
  • i really need some more data, who match the raw data and the wanted output. actually they don't match. – Nina Scholz Jun 23 '17 at 06:51
  • No worries Nina, I found that I asked the question in a confusing manner and apologize for taking up your time. – pertrai1 Jun 23 '17 at 07:05
0
let allStores = storesObject.stores.store;

With code above you will create new reference to already existed array of store objects.

Fabio
  • 31,528
  • 4
  • 33
  • 72