0

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

 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' } ]
1
  • So what's the problem? Do you know how to iterate over an array? And how to add properties to an object? That's the only thing you'd need to know... Commented Sep 22, 2015 at 0:57

1 Answer 1

2
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.

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

1 Comment

tks your function is perfect as I can then add to the a array and it will still work where as manually constructing it won't be as dynamic

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.