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

javascript - Merge two arrays into an array of objects with property values - Stack Overflow

programmeradmin0浏览0评论

Need to generate an array as below:

[
  { applicationName :'appName1', frequency:'00'},
  { applicationName :'appName2', frequency:'3'},
  { applicationName :'appName3', frequency:'25'},
  { applicationName :'appName4', frequency:'54'}
]

scope.appApplications - it's an object that I am splitting into two arrays. I have two arrays as below. Need to merge them (applicationName[] and frequencies[]) and e with output as above. How can this be done?

var frequencies = [];
var applicationName = [];
angular.forEach(scope.appApplications, function (value, key) {
           frequencies.push(value);
           applications.push(key);
 })

Need to generate an array as below:

[
  { applicationName :'appName1', frequency:'00'},
  { applicationName :'appName2', frequency:'3'},
  { applicationName :'appName3', frequency:'25'},
  { applicationName :'appName4', frequency:'54'}
]

scope.appApplications - it's an object that I am splitting into two arrays. I have two arrays as below. Need to merge them (applicationName[] and frequencies[]) and e with output as above. How can this be done?

var frequencies = [];
var applicationName = [];
angular.forEach(scope.appApplications, function (value, key) {
           frequencies.push(value);
           applications.push(key);
 })
Share Improve this question edited Dec 6, 2023 at 7:25 mplungjan 178k28 gold badges181 silver badges240 bronze badges asked Apr 26, 2015 at 14:38 Avs_90Avs_90 491 silver badge6 bronze badges 4
  • 5 There's no such thing as "JSON object". JSON is like XML. It's a string. – Oleg Commented Apr 26, 2015 at 14:40
  • can you show the structure of $scope.applications? – Kop4lyf Commented Apr 26, 2015 at 14:40
  • Are you saying that frequencies and applicationName are populated by scope.appApplications and then from those two arrays you want to create the desired merged array? – Jason Cust Commented Apr 26, 2015 at 15:22
  • Need to generate a JSON as below - That is not valid JSON. Try using (for example) jsonformatter. – Henke - Нава́льный П с м Commented May 21, 2021 at 9:01
Add a ment  | 

3 Answers 3

Reset to default 5

If you're willing to use underscore, you can do this as a single chained functional call:

_.zip(applicationName,frequencies).map(function(pair) { 
    return _.object(["applicationName","frequency"],pair); 
});

_.zip() turns ['a','b','c'] and [1,2,3] into [['a',1],['b',2],['c',3]].

Array.map() calls a function on each member of an array and returns an array of the results.

_.object() turns ["applicationName","frequency"] and ['a',1] into {applicationName: 'a', frequency: 1}.

You can have an another variable (say targetJson) which will hold your target json. something like this.

var frequencies = [];
var applicationName = [];
var targetJson = [];
angular.forEach(scope.appApplications, function (value, key) {
           frequencies.push(value);
           applications.push(key);
           targetJson.push({applicationName :key, frequency:value});
 })

Assuming scope.appApplications is an object where the keys are the application names and the values are the frequencies, you could do something like:

var frequencies = [];
var applicationName = [];
var mergedArray = Object.keys(scope.appApplications).map(function (key) {
  frequencies.push(scope.appApplications[value]);
  applications.push(key);

  return {
    applicationName: key,
    frequency: scope.appApplications[value]
  };
});
发布评论

评论列表(0)

  1. 暂无评论