4

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 come 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);
 })
4
  • 5
    There's no such thing as "JSON object". JSON is like XML. It's a string. Commented Apr 26, 2015 at 14:40
  • can you show the structure of $scope.applications? 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? Commented Apr 26, 2015 at 15:22
  • Need to generate a JSON as below - That is not valid JSON. Try using (for example) jsonformatter.org. Commented May 21, 2021 at 9:01

3 Answers 3

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

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

2 Comments

This, or its equivalent in lodash, is how I'd do it. +1.
Note that since lodash v0.4.0 it is now zipObject lodash.com/docs#zipObject
2

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});
 })

1 Comment

JSON is a string.
1

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]
  };
});

Comments

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.