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"
       },
  // ...
}
4
  • Possible duplicate of Javascript underscore array to object Commented Dec 23, 2017 at 20:32
  • @GeorgeJemptyI could not think of a better title but the question has a slight difference. Commented Dec 23, 2017 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 Commented Dec 23, 2017 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 Commented Dec 23, 2017 at 21:09

1 Answer 1

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

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

10 Comments

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 )
which type to select as a key if you have multiple types? and for the value, will it be an array?
you will have multiple keys in the object with values that come from the params
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
i got what you want! check now how it is done. here is a link on the main problem i think you are facing stackoverflow.com/questions/11508463/…
|

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.