0

I am learning underscore now and found a task for it I need help with .. I have an array with object looking like this

[
    // ...
    {
            "type": "presence",
            "params": {
                "interval": 15,
                "foo": "something",
                "link": {
                    "fp_type": "1",
                    "fp_ext_id": "2"
            },
    },
    {
            "type": "bar",
            "params": {
                "interval": 30,
                "foo": "foo",
                "link": {
                    "fp_type": "2",
                    "fp_ext_id": "3"
                },
            },
    },
    // ...
]

The task is using underscore only to convert this array items to an object where the the key is the items type and the value are its params, i.e.:

{
  // ...
  "presence": {
    "interval": 15,
    "foo": "something",
    "link": {
        "fp_type": "1",
        "fp_ext_id": "2"
     },
  }, 
  "bar": {
       "interval": 30,
       "foo": "foo",
       "link": {
           "fp_type": "2",
           "fp_ext_id": "3"
       },
  // ...
}
Dexygen
  • 12,287
  • 13
  • 80
  • 147
Yordan Kanchelov
  • 511
  • 9
  • 26
  • Possible duplicate of [Javascript underscore array to object](https://stackoverflow.com/questions/22427560/javascript-underscore-array-to-object) – Dexygen Dec 23 '17 at 20:32
  • @GeorgeJemptyI could not think of a better title but the question has a slight difference. – Yordan Kanchelov Dec 23 '17 at 20:40
  • 1
    If it only has a slight difference you should at least be able to use that to show us some code you actually tried – Dexygen Dec 23 '17 at 20:42
  • the closest solution I got to this was something like the answer of @hd84335 `var y = _.map(x, function(i) { let obj = {}; obj[i.type] = i.params; return obj; }); var result = Object.assign({}, ...y);` But I still don't like the solution and think that is not okay with the assignment – Yordan Kanchelov Dec 23 '17 at 21:09

1 Answers1

2

You can do it this way:

var x = [    
    {
            "type": "presence",
            "params": {
                "interval": 15,
                "foo": "something",
                "link": {
                    "fp_type": "sponsor",
                    "fp_ext_id": "spotme"
                },
            },
    },
    {
            "type": "bar",
            "params": {
                "interval": 30,
                "foo": "foo",
                "link": {
                    "fp_type": "2",
                    "fp_ext_id": "3"
                },
            },
    }
];

var y = _.map(x, function(i) {
  let obj = {};
  obj[i.type] = i.params;
  return obj;
});
//console.log(y);

var result = y.reduce(function(obj,item) { 
  obj[_.keys(item)[0]] = _.values(item)[0]; 
  return obj;
}, {});
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>

DEMO

hd84335
  • 8,815
  • 5
  • 34
  • 45
  • this returns array with 2 objects in this example. the point is to convert the array to a single object with key which is the type and value which are the params ( they may be nested ) – Yordan Kanchelov Dec 23 '17 at 16:30
  • which type to select as a key if you have multiple types? and for the value, will it be an array? – hd84335 Dec 23 '17 at 16:33
  • you will have multiple keys in the object with values that come from the params – Yordan Kanchelov Dec 23 '17 at 16:40
  • I updated the example to help you understand the task :). the second code snipped should be the returned object composed from the first code snipped – Yordan Kanchelov Dec 23 '17 at 17:02
  • i got what you want! check now how it is done. here is a link on the main problem i think you are facing https://stackoverflow.com/questions/11508463/javascript-set-object-key-by-variable – hd84335 Dec 23 '17 at 19:12
  • It's almost the desired result. The first thing that I find is that this returns an object which is array and you can access it with y[0] or y[1]. in the requirements the output should be an object and in this example, the top key values should be "presence1" and "bar". Thanks for the effort trying to help me ! – Yordan Kanchelov Dec 23 '17 at 20:09
  • mm here it is... this question is tricky, thanks to ES6 which provides a method that makes the conversion! – hd84335 Dec 23 '17 at 20:32
  • Yes, it is :) I kinda feel like this is a cheat, in the end, using Object.assign to convert it to an object. Anyway if no one gives a better solution soon I will accept your answer – Yordan Kanchelov Dec 23 '17 at 20:40
  • mmm it's not a cheat, that's ECMA6 official! Anyways, if you don't want to use it, we can break it down and use pure `underscorejs` to solve this. i update – hd84335 Dec 23 '17 at 22:46
  • If you can I will be glad, because I am kinda new underscore. The assignment is to use only underscore – Yordan Kanchelov Dec 23 '17 at 22:56