如何将2个数组连接到节点中的单个json/数组

时间:2022-09-21 00:59:10

I have 2 arrayes in node .

节点中有两个数组。

['3', '7' ]

[' 3 ',' 7 ']

[ 'Circulatory and Cardiovascular', 'Respiratory' ]

['循环和心血管','呼吸']

I want to produce result as below.

我想把结果写在下面。

{{"id": "3","name":"Circulatory and Cardiovascular"},{"id": "7","name":"Respiratory"}}

{ { " id ":“3”,“名字”:“循环和心血管”},{ " id ":" 7 ","名称":"呼吸" } }

2 个解决方案

#1


2  

I assume you want to create this data structure (array instead of hash):

我假设您想创建这个数据结构(数组而不是散列):

[{"id": "3","name":"Circulatory and Cardiovascular"},{"id": "7","name":"Respiratory"}]

[{ " id ":“3”,“名字”:“循环和心血管”},{ " id ":“7”,“名字”:“呼吸”}]

In that case you can use lodash like this:

在这种情况下,你可以使用这样的破折号:

var _ = require('lodash');

var a1 = ['3', '7' ];
var a2 = [ 'Circulatory and Cardiovascular', 'Respiratory' ];

var obj = _.merge(_.map(a1, function (o) { return { id : o } }), _.map(a2, function (o) { return { name : o } }))

console.log(obj);

#2


1  

I'm sorry that's an array in output but it's easier. You could do this:

很抱歉,这是输出中的数组,但它更简单。你可以这样做:

var idArray = ['3', '7' ];
var nameArray = [ 'Circulatory and Cardiovascular', 'Respiratory' ];
var newArray = [];

for (var i = 0; i < idArray.length; i++) {
    newArray.push({"id": idArray[i], "name": nameArray[i]});
}

Output:

输出:

[ { id: '3', name: 'Circulatory and Cardiovascular' },
  { id: '7', name: 'Respiratory' } ]

I'm not sure that's a great idea, but you can convert your new array into an object like this:

我不确定这是不是一个好主意,但是你可以把你的新数组转换成这样的对象:

var newObject = newArray.reduce(function(o, v, i) {
  o[i] = v;
  return o;
}, {});

Output:

输出:

{ '0': { id: '3', name: 'Circulatory and Cardiovascular' },
  '1': { id: '7', name: 'Respiratory' } }

Or another way:

或另一种方式:

Object.setPrototypeOf(newArray, Object.prototype); // Now it's an object

Output:

输出:

Object [
  { id: '3', name: 'Circulatory and Cardiovascular' },
  { id: '7', name: 'Respiratory' } ]

Hope this help !

希望这次的帮助!

#1


2  

I assume you want to create this data structure (array instead of hash):

我假设您想创建这个数据结构(数组而不是散列):

[{"id": "3","name":"Circulatory and Cardiovascular"},{"id": "7","name":"Respiratory"}]

[{ " id ":“3”,“名字”:“循环和心血管”},{ " id ":“7”,“名字”:“呼吸”}]

In that case you can use lodash like this:

在这种情况下,你可以使用这样的破折号:

var _ = require('lodash');

var a1 = ['3', '7' ];
var a2 = [ 'Circulatory and Cardiovascular', 'Respiratory' ];

var obj = _.merge(_.map(a1, function (o) { return { id : o } }), _.map(a2, function (o) { return { name : o } }))

console.log(obj);

#2


1  

I'm sorry that's an array in output but it's easier. You could do this:

很抱歉,这是输出中的数组,但它更简单。你可以这样做:

var idArray = ['3', '7' ];
var nameArray = [ 'Circulatory and Cardiovascular', 'Respiratory' ];
var newArray = [];

for (var i = 0; i < idArray.length; i++) {
    newArray.push({"id": idArray[i], "name": nameArray[i]});
}

Output:

输出:

[ { id: '3', name: 'Circulatory and Cardiovascular' },
  { id: '7', name: 'Respiratory' } ]

I'm not sure that's a great idea, but you can convert your new array into an object like this:

我不确定这是不是一个好主意,但是你可以把你的新数组转换成这样的对象:

var newObject = newArray.reduce(function(o, v, i) {
  o[i] = v;
  return o;
}, {});

Output:

输出:

{ '0': { id: '3', name: 'Circulatory and Cardiovascular' },
  '1': { id: '7', name: 'Respiratory' } }

Or another way:

或另一种方式:

Object.setPrototypeOf(newArray, Object.prototype); // Now it's an object

Output:

输出:

Object [
  { id: '3', name: 'Circulatory and Cardiovascular' },
  { id: '7', name: 'Respiratory' } ]

Hope this help !

希望这次的帮助!