1

What is the easiest way to convert the former to the latter?

var simpleArray = [1,1,2,3,5,8,13,21];


var objArray = [{ tag : 1}, { tag : 1},{ tag : 2}, { tag : 3},
                { tag : 5}, { tag : 8},{ tag : 13},{ tag : 21}];

1 Answer 1

4

No need for underscore. You can do this pretty easily with vanilla javascript:

var i, tag, main, simpleArray = [1,1,2,3,5,8,13,21];

var complexArray = simpleArray.map(function(e) { return { tag: e }; });

main = document.getElementById('main');

for(i = 0; i < complexArray.length; i++) {
  tag = complexArray[i];
  main.innerHTML += '<div>tag: '+tag.tag+'</div>';
}
<div id='main'></div>

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.