0

I have an array with key value sequence (first element is a key, second - value etc.)

var arr = ['a',1,'b',2,'c',3];

I need to convert this array to object using lodash. How can I do this?

result = {
  a : 1,
  b : 2,
  c : 3
};

1 Answer 1

0

If it's every other value, you could just reduce that

var arr = ['a',1,'b',2,'c',3];

var obj = arr.reduce( (a,b,i) => {return i%2===0 ? (a[arr[i]] = arr[i+1],a) : a}, {});

console.log(obj)

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

Comments

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.