0

I have 2 arrays, array a contains keys and array b contains their values

let a = ['name', 'options', 'address', 'options', 'gender', 'options'];
let b = ['john doe', 'a', 'india', 'b', 'male', 'c'];

I want output like this

 { 
  'name': 'john doe',
  'options': 'a, b, c',
  'address': 'india',
  'gender': 'male'
 }
2
  • 5
    How do you plan on having a, b, c map to options? Seems like you're going to have to do this manually Commented Jun 6, 2018 at 12:30
  • 2
    the a array contains options trice, so it's still one-to-one mapping. Commented Jun 6, 2018 at 12:32

4 Answers 4

3

Use Array.reduce

let a = ['name', 'options', 'address', 'options', 'gender', 'options'];
let b = ['john doe', 'a', 'india', 'b', 'male', 'c'];

let r = a.reduce((o,c,i) => {o[c] = o[c] ? o[c] + ", " + b[i]:b[i]; return o;}, {})
console.log(r);

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

Comments

1

An alternative method would be as shown below. This loops for a.length amount of times, gets the value of a and adds it as a key to c, where the key's value is the value from b. Here is a breakdown of how it works:

For each element in a: if there exists no element in c with the keyname a[i], create the key and set it to the value from b[i]. If the element does already exist in c (then the key must represent an array, not a single item), if that value is a single item, make it an array, and re insert that first value, then in any case, push the new item to that array.

let a = ['name', 'options', 'address', 'options', 'gender', 'options','options'];
let b = ['john doe', 'a', 'india', 'b', 'male', 'c', 'd'];

let c = {};

for (let i=0; i<a.length; i++) {
    if (typeof c[a[i]] === 'undefined') {
        c[a[i]] = b[i];
    } else {
        if (c[a[i]] instanceof Array === false) {
            c[a[i]] = [c[a[i]]];
        }
        c[a[i]].push(b[i]);
    }
}

console.log(c);

Comments

0
  invert(object){ 
    let invertedObject ={};
    for(let key in object){
      const originalValue = object[key];
      invertedObject = {originalValue : key}
    }
    return invertedObject
  }

1 Comment

Please explain why the problem should be solve this way.
0
let a = ['name', 'options', 'address', 'options', 'gender', 'options'];
let b = ['john doe', 'a', 'india', 'b', 'male', 'c'];

let r = a.reduce((o,c,i) => {o[c] = o[c] ? o[c] + ", " + b[i]:b[i]; return o;}, [])
console.log(r);

2 Comments

Please don't post only code as answer, but provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are generally of higher quality, and are more likely to attract upvotes.
Please edit your answer and provide a brief description because StackOverflow puts answers without a brief explanation on the Low Quality posts review queue to see if they should be deleted... The reason is thousands of people skim answers in a hurry to find the one that best solvers their problem and it wastes the time of a lot of people if they have to spend a lot of time studying answers to figure out if it's really the one they need, and have to read the question, reverse engineer your answer and possibly read language documentation to figure out what your fix solves or how it works.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.