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

How to combine two arrays into an array of objects in JavaScript? - Stack Overflow

programmeradmin0浏览0评论

I have two arrays and I want to form an array of objects such that the new array of obj has two keys with first key being filled with elements of first array and second key having arrays elements of second array. Can this be done using the map function. I found the closest answer as this:- Merge two arrays into an array of objects with property values

eg.:-

ar1=[];
ar2=[];
armixed=[{ar1element,ar2element},{}......]

But it uses angular JS I just want to use pure JS.

I have two arrays and I want to form an array of objects such that the new array of obj has two keys with first key being filled with elements of first array and second key having arrays elements of second array. Can this be done using the map function. I found the closest answer as this:- Merge two arrays into an array of objects with property values

eg.:-

ar1=[];
ar2=[];
armixed=[{ar1element,ar2element},{}......]

But it uses angular JS I just want to use pure JS.

Share Improve this question edited May 15, 2023 at 16:38 isherwood 61.1k16 gold badges120 silver badges168 bronze badges asked Jun 7, 2018 at 12:46 heartyhearty 7315 gold badges10 silver badges15 bronze badges 6
  • We need to see the 2 arrays you're working with, and the end result of what you want them to be, plus possibly the code you've already attempted to use. – castis Commented Jun 7, 2018 at 12:47
  • The question you are referring to has the answer - in plain JS ... – JSON Derulo Commented Jun 7, 2018 at 12:49
  • Can this be done using the map function? Already answered with map in referrer question. – Nishant Dixit Commented Jun 7, 2018 at 12:51
  • Hi there. I see you've just edited your question with some code, which is good, but the code you've provided doesn't give any real clue as to what you're trying to accomplish. Have a look at how to write a Minimal, Complete, and Verifiable Example. In addition, the question you linked doesn't seem to have anything to do with Angular, and in any case merging arrays would be accomplished using "pure" JS even if it were. Can you explain what threw you off there? – Fissure King Commented Jun 7, 2018 at 12:51
  • I am gettingscope is undefined – hearty Commented Jun 7, 2018 at 12:56
 |  Show 1 more comment

2 Answers 2

Reset to default 17

I'm not sure what your output should be but the one you provided seems invalid. I have modified the output format to be valid.

For the task you have the solution is to zip the arrays, however, JS has no inbuilt zip function, so we can emulate it via map function:

var ar1 = ['a1', 'a2', 'a3', 'a4', 'a5'];
var ar2 = ['b1', 'b2', 'b3', 'b4', 'b5'];
var armixed = ar1.map(function(x, i) {
  return [x, ar2[i]]
});

console.log(armixed);

Output will be:

armixed = [
    ["a1", "b1"]
    ["a2", "b2"]
    ["a3", "b3"]
    ["a4", "b4"]
    ["a5", "b5"]
]

If you want objects in your output (rather than arrays), you can just edit the return statement above to something like:

return { categories: x, catid: ar2[i] }

Nowadays we can write this to make an array of objects from multiple arrays

const price = ['100', '200', '300', '400', '500'];
const qty = ['50', '40', '30', '20', '10'];

const result = price.map((price,i) => ({price, qty:qty[i]}))

console.log(result)

发布评论

评论列表(0)

  1. 暂无评论