0

Not sure if there's any lodash or other util method to map object sequence. I have this case where I have to manually reorder the order of bunch of nested objects.

//unprocessed
       {
           "order": [
          "home",
          "work"
        ],
        "questions": {
          "US": {
            "work": "working",
            "home": "driving home"
          },
          "UK": {
            "work": "go to work",
            "home": "go to home"
          }
        }
    }

How can I map questions's object's object's answer base on order's value. The processed output should be

{
    "questions": {
      "US": {
        "home": "driving home",
        "work": "working"
      },
      "UK": {
        "home": "go to home",
        "work": "go to work"
      }
    }
}
  • what do you mean by `reorder the order of bunch of nested objects`? – Vivek Jun 29 '18 at 09:15
  • 3
    You can't control the order of objects, for that you need a _map_ or _array_ – Asons Jun 29 '18 at 09:16
  • @Vivek see the relationship of the `order` and `question`, the first one is unprocessed, the second block of code is expected output. –  Jun 29 '18 at 09:19
  • @LGSon I don't think so https://stackoverflow.com/a/31102605/9728810 –  Jun 29 '18 at 09:20
  • Check this: https://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order – choasia Jun 29 '18 at 09:21
  • @Melissa92 i can only see `keys` of "US" and "UK" are in order with "order" values, if this is what you want then it makes no sense as order does not matter in Objects, it matters only in Arrays – Vivek Jun 29 '18 at 09:21
  • 1
    You don't think what? ... that one can't order an object? ... then also check second answer in your link, and do note, the _ojbect_ is not sorted, they sort it while retrieving data from it, which is a different story. – Asons Jun 29 '18 at 09:26
  • @LGSon I see!! so what should I do? I don't have choice, someone coded the api that way, now I should map them into my own format? –  Jun 29 '18 at 09:49
  • Do what they do in that link, sort it when you get it, and when stored, and no one is looking, it doesn't matter how it's ordered – Asons Jun 29 '18 at 09:51
  • To recap the above comments. You **cannot** force the order of the properties in the actual object. But you can access them in the order you want by using the `order` array you already have. – Gabriele Petrioli Jun 29 '18 at 09:54
  • @GabrielePetrioli access them is pointless, I have to display them in the right order in the client, should I map the questions into array so that I can ensure the correct order? –  Jun 29 '18 at 09:56
  • Does the client need to see the actual JSON or render some UI based on its contents ? – Gabriele Petrioli Jun 29 '18 at 09:57
  • I decided to delete my answer as it obviously weren't useful enough. I also noticed that you more or less never accept, nor upvote given answers, and one is suppose to, and therefore I will opt out from answering anymore of your questions, unless you start doing that. – Asons Jul 03 '18 at 21:13

4 Answers4

0

"questions": { "US": { "work": "working", "home": "driving home" }, "UK": { "work": "go to work", "home": "go to home" } } is same "questions": { "US": { "home": "driving home", "work": "working" }, "UK": { "home": "go to home", "work": "go to work" } } so you don't to do anything

Peng
  • 38
  • 4
  • when you display it, how you ensure the order of `question` follow the `order`? –  Jun 29 '18 at 09:50
0

If you actually want it in an order then I'll prefer you should arrays to store home and work.

Eg:

questions : { US : [ 'drivung home', ' working' ] }

0

I don't get why you want to order a key-value pair. But I have done that for printing once. See the code below.

var data = { name:'stack',
             age:40, 
             grant:6000


};
console.log(Object.entries(data).sort(function(a, b) {<SORT LOGIC>}).reduce( (o,[k,v]) => (o[k]=v,o), {} ));
vizsatiz
  • 1,933
  • 1
  • 17
  • 36
  • compare code 1 and code 2 you will get it, I think the code is clear –  Jun 29 '18 at 09:48
0

Here is some code to access them in the order specified in the order array to display them.

const data = {"order": ["home","work"],"questions": {"US": {"work": "working","home": "driving home"},"UK": {"work": "go to work","home": "go to home"}}};

// using/accessing the data to display to client
const {order:orderedAccess, questions} = data;

Object.keys(questions).map(country=>
  orderedAccess.forEach(action=>console.log(country, action, questions[country][action]))
)
Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317