简体   繁体   English

尝试创建给定2个数组+映射的对象数组

[英]trying to create an array of objects given 2 arrays + map

I have the following 2 arrays here: 我在这里有以下两个数组:

  > chNameArr
[ 'chanel1',
  'chanel2',
  'chanel3',
  'chanel4',
  'chanel5',
  'chanel6',
  'chanel7' ]

and here: 和这里:

   > a
[ 'channelName'
  'status',
  'connections',
  'socketIds',
  'lastRun',
  'numberOfRuns',
  'timeout' ]

what I am trying to achieve is the following objects per channel in an array where channelName from a get the value from chNameArr but the rest of 'a' gets an empty string 我试图做到的,是在一个阵列每个通道中的下列对象,其中channelNamea来自获得的价值chNameArr但其余“A”得到一个空字符串

 file=[{"channelName":"chanel1","status":"", "connections":"", "socketIds":"", "lastRun":"", "numberOfRuns":"", "timeout":""},
 .
 .
 .
        {"channelName":"chanel7","status":"", "connections":"", "socketIds":"", "lastRun":"", "numberOfRuns":"", "timeout":""}]

this is my attempt 这是我的尝试

   > chNameArr.map(function(d){return {channelName:d}})
[ { channelName: 'chanel1' },
  { channelName: 'chanel2' },
  { channelName: 'chanel3' },
  { channelName: 'chanel4' },
  { channelName: 'chanel5' },
  { channelName: 'chanel6' },
  { channelName: 'chanel7' } ]
chNameArr.map(function(d) {
  result = {};
  result[a[0]] = d;
  for (var i=1; i<a.length; i++) {
    result[a[i]] = "";
  }
  return result;
})

There is no one-liner to solve this problem in general, although if you don't actually need to use the array a you could manually construct {channelName:d, status: "", ...} in your original map. 一般而言,没有一种方法可以解决此问题,尽管如果您实际上不需要使用数组a ,则可以在原始映射中手动构造{channelName:d, status: "", ...}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM