最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to combine 2 arrays into 1 object javascript - Stack Overflow

programmeradmin7浏览0评论

I have 2 arrays that i want to "reduce" into 1 object.

so for instance:

I have..

var originalData = [1,2,3];

var newData = [3,1,2];

i want to bine these two arrays into an object that looks like this:

var newOrderObject = {1:3, 2:1, 3:2};

im using a reduce function but it seems to be mapping them differently

var newOrderObject = originalData.reduce(function(newData) {
  var orderObject = {};
  originalData = newData;
  return orderObject;
}, {});

any suggests would be appretiated

I have 2 arrays that i want to "reduce" into 1 object.

so for instance:

I have..

var originalData = [1,2,3];

var newData = [3,1,2];

i want to bine these two arrays into an object that looks like this:

var newOrderObject = {1:3, 2:1, 3:2};

im using a reduce function but it seems to be mapping them differently

var newOrderObject = originalData.reduce(function(newData) {
  var orderObject = {};
  originalData = newData;
  return orderObject;
}, {});

any suggests would be appretiated

Share Improve this question edited Jan 11, 2019 at 21:40 Charlie Schliesser 8,2555 gold badges48 silver badges79 bronze badges asked Jan 11, 2019 at 21:19 zomdarzomdar 2732 gold badges7 silver badges24 bronze badges 3
  • Have you tried this? stackoverflow./questions/10623635/… – Pietro Nadalini Commented Jan 11, 2019 at 21:21
  • i'd like to merge into an object...i dont think that i can use push() right? – zomdar Commented Jan 11, 2019 at 21:22
  • Possible duplicate of Creating a JavaScript Object from two arrays – Pietro Nadalini Commented Jan 11, 2019 at 21:27
Add a ment  | 

3 Answers 3

Reset to default 6

Reduce the 1st array (the keys), and take the values from the 2nd array using the index:

var originalData = [1,2,3];
var newData = [3,1,2];

var newOrderObject = originalData.reduce(function(obj, key, index) {
  obj[key] = newData[index];
  return obj;
}, {});

console.log(newOrderObject);

You're just using .reduce wrong.

  • The 1st argument of it's callback is the accumulator object you pass.
  • The 2nd is the currently iterated item.
  • The 3rd is the index.

Use all 3 and you get the result you want.

var originalData = [1,2,3];
var newData = [3,1,2];

var newOrderObject = originalData.reduce(function(acc, item, i) {
  return Object.assign(acc, { [item]: newData[i] })
}, {});

console.log(newOrderObject)

You could map single objects and assign them to a single object. If empty arrays are possible, use an empty object as target value.

var keys = [1, 2, 3],
    values = [3, 1, 2],
    object = Object.assign({}, ...keys.map((k, i) => ({ [k]: values[i] })));

console.log(object);

A more cleaner approach would be to transpose the arrays to an array of key/value pairs and then map object.

const transpose = (r, a) => a.map((v, i) => (r[i] || []).concat(v));

var keys = [1, 2, 3],
    values = [3, 1, 2],
    object = Object.assign(
        {},
        ...[keys, values]
            .reduce(transpose, [])
            .map(([k, v]) => ({ [k]: v }))
    );

console.log(object);

发布评论

评论列表(0)

  1. 暂无评论