Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9148987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:24:56+00:00 2026-06-17T11:24:56+00:00

Given two arrays, one with keys, one with values: keys = [‘foo’, ‘bar’, ‘qux’]

  • 0

Given two arrays, one with keys, one with values:

keys = ['foo', 'bar', 'qux']
values = ['1', '2', '3']

How would you convert it to an object, by only using underscore.js methods?

{
   foo: '1', 
   bar: '2',
   qux: '3'
}

I’m not looking for a plain javascript answer (like this).

I’m asking this as a personal exercise. I thought underscore had a method that was doing exactly this, only to find out it doesn’t, and that got me wondering if it could be done.
I have an answer, but it involves quite a few operations. How would you do it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T11:24:57+00:00Added an answer on June 17, 2026 at 11:24 am

    What you need to use is the _.object method of underscore js.
    If object method is not present in your version of underscore.js then you will have to manually add this method to it.

    keys = ['foo', 'bar', 'qux']
    values = ['1', '2', '3']
    _.object = function(list, values) {
      if (list == null) return {};
      var result = {};
      for (var i = 0, l = list.length; i < l; i++) {
        if (values) {
          result[list[i]] = values[i];
        } else {
          result[list[i][0]] = list[i][1];
        }
      }
      return result;
    };
    
    console.log(_.object(keys, values))
    <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given two arrays, how do you check if one is a cyclic permutation of
There are two arrays, that are given for $link from foreach . One time
Given two object arrays I need to compare the differences between the two (when
Given two integer arrays of size N, design an algorithm to determine whether one
Given two arrays, how to find the maximum element which is common to both
Given two sorted arrays of integers, a and b , and an integer c
Given two sorted arrays: A and B . The size of array A is
Given two struct arrays A and B with field f1: A = struct('f1',{1,2,3}) B
Given a basic key/value array, I'm wanting to store two sorted arrays based on
Given two arrays int arr1[n] int arr2[m] where n > m Need to write

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.