javascript:隐式匹配两个javascript对象并创建一个对象

时间:2018-05-08 13:09:55

标签: javascript arrays json

我是javascript和json对象的新手。我正在处理一个数组,其中每个元素如下所示:

 input = { 
      clgid: 135,
      careerlevelid: 10000130,
      metricname: 'joiners',
      monthnames: '201810,201811,201812,20184,20185,....',
      metricvalue: '18.00,0.00,7.00,15.00,0.00,0.00,....' 
      }

monthanames - 可以是1到24之间的任何

metricvalue - 是该月份名的相应值

我想要一个如下的输出:

output = { 
  clgid: 135,
  careerlevelid: 10000130,
  metricname: 'joiners',
   201810: 18.00,
  201811: 0.00,
  201812: 7.00,
  201804: 15.00,
  201805: 0.00,

}

Iam尝试使用for循环()和TSMap(https://www.npmjs.com/package/typescript-map)来做这件事,但不知怎的,我无法获得所需的输出。

3 个答案:

答案 0 :(得分:0)

您可以使用Map创建新属性。

let input = { 
      clgid: 135,
      careerlevelid: 10000130,
      metricname: 'joiners',
      monthnames: '201810,201811,201812,20184,20185',
      metricvalue: '18.00,0.00,7.00,15.00,0.00,0.00' 
      };
      
let months = input.monthnames.split(',');
let values = input.metricvalue.split(',');
input.metricname = [input.metricname, ...months.map((month, i) => month+':'+values[i])];



console.log(input);

答案 1 :(得分:0)

使用destructuring with rest获取monthnamesmetricvaluebase(其他媒体资源)。

String.split() monthnamesmetricvalue到数组。

使用Array.map()来迭代months。从values获取相应的值,然后创建一个对象。

使用`Object.assign()array spreadbase结合使用。

注1:月份将按其键的数值排序。

注2:将值转换为数字会删除冗余小数点。



const input = {
  clgid: 135,
  careerlevelid: 10000130,
  metricname: 'joiners',
  monthnames: '201810,201811,201812,20184,20185',
  metricvalue: '18.00,0.00,7.00,15.00,0.00,0.00'
};

const reformatMonthes = (obj) => {
  const { monthnames, metricvalue, ...base } = input;
  const months = monthnames.split(',');
  const values = metricvalue.split(',');
  
  return Object.assign(
    base,
    ...months.map((m, i) => ({
      [m]: +values[i]
    }))
  );
};
console.log(reformatMonthes(input));




为了保留订单,最好使用月份对象数组:



const input = {
  clgid: 135,
  careerlevelid: 10000130,
  metricname: 'joiners',
  monthnames: '201810,201811,201812,20184,20185',
  metricvalue: '18.00,0.00,7.00,15.00,0.00,0.00'
};

const reformatMonthes = (obj) => {
  const { monthnames, metricvalue, ...base } = input;
  const months = monthnames.split(',');
  const values = metricvalue.split(',');
  
  return {
    ...base,
    months: months.map((m, i) => ({
      [m]: +values[i]
    }))
  };
};
console.log(reformatMonthes(input));




答案 2 :(得分:0)

试试这个:



var jsonObj = {
	"clgid": 135,
	"careerlevelid": 10000130,
	"metricname": "joiners",
	"monthnames": "201810,201811,201812,20184,20185",
	"metricvalue": "18.00,0.00,7.00,15.00,0.00"
};

// Split month names string to convert it into an array.
var monthnames = jsonObj.monthnames.split(',');

// Split metric value string to convert it into an array.
var metricvalue = jsonObj.metricvalue.split(',');

// delete monthnames property from an jsonObj.
delete jsonObj.monthnames;

// delete metricvalue property from an jsonObj.
delete jsonObj.metricvalue;

// iterate monthnames to assign the array elements as key in jsonObj.
for (var i in monthnames) {
    jsonObj[monthnames[i]] = metricvalue[i];
}

// Output
console.log(jsonObj);