将两个数组合并为具有属性值的对象数组
问题描述:
需要生成如下JSON:
Need to generate a JSON as below:
{ applicationName :'appName1', frequency:'00'},
{ applicationName :'appName2', frequency:'3'},
{ applicationName :'appName3', frequency:'25'},
{ applicationName :'appName4', frequency:'54'}
scope.appApplications - 它是一个JSON对象,我将其拆分为两个数组。
我有两个数组,如下所示。需要合并它们(applicationName []和频率[])并带有如上所述的输出。怎么办呢?
scope.appApplications - its a JSON 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);
})
答
假设 scope.appApplications
是一个对象,其中键是应用程序名称,值是频率,你可以这样做:
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]
};
});