-5

I have this

array = ["column1", "column2", "column3"]

and this other

array = ["infor1", "infor2", infor3"]

I would like to do this:

{
  column1: infor1,
  column2: infor2,
  column3: infor3
}
4
  • We expect a little bit of effort, please show what you have tried to achieve your objective. Commented Jan 29, 2020 at 11:37
  • Have you tried anything? Commented Jan 29, 2020 at 11:37
  • Google is your friend. stackoverflow.com/questions/54789406/… Commented Jan 29, 2020 at 11:37
  • Oops. I didn't notice that. @MikeK Commented Jan 29, 2020 at 11:39

2 Answers 2

2

You can try to do something like this,

const props = ["column1", "column2", "column3"];
const values = ["infor1", "infor2", "infor3"];

props.reduce((acc, itm, i) => (acc[itm] = values[i], acc) , {});

You can read more about .reduce here.

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

2 Comments

@carlosSanchez You can refer the question that was used to duplicate your question . But this is a complete answer.
short and neat answer
0

var array1 = ["column1", "column2", "column3"]

var array2 = ["infor1", "infor2", "infor3"]

var obj = {};
array1.forEach(function(y, z) {
  obj[y] = array2[z];

});
console.log(obj)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.