简体   繁体   English

将数据从一种对象数组转换为另一种对象

[英]Converting data from an array of objects of one kind to another

There is a set of data that comes dynamically from the server and looks like this:有一组来自服务器的动态数据,如下所示:

dataSet = [
 {vid: "General uneven corrosion", mk: "Visual measurement control",  ver: "95"}, 
 {vid: "General uneven corrosion", mk: "Ultrasonic thickness measurement", ver: "95"},
 {vid: "General uneven corrosion", mk: "Ultrasonic flaw detection", ver: "80"},
 {vid: "General uneven corrosion", mk: "Radiographic control", ver: "80"},
 {vid: "General uneven corrosion", mk: "Eddy current control", ver: "60"},
 {vid: "General uneven corrosion", mk: "Magnetic control", ver: "20"},
 {vid: "General uneven corrosion", mk: "Metallographic research", ver: "20"},
 {vid: "Increase in strength characteristics", mk: "Mechanical property studies", ver: "80"},
 {vid: "Increase in strength characteristics", mk: "Coercimetry", ver: "40"},
 {vid: "Increase in strength characteristics", mk: "Metallographic research", ver: "40"},
 {vid: "Increase in hardness", mk: "Mechanical property studies", ver: "95"},
 {vid: "Increase in hardness", mk: "Coercimetry", ver: "60"},
 {vid: "Increase in hardness", mk: "Metallographic research", ver: "40"},
 {vid: "Decreased ductility", mk: "Coercimetry", ver: "60"},
 {vid: "Decreased ductility", mk: "Mechanical property studies", ver: "60"},
 {vid: "Decreased ductility", mk: "Acoustic emission control", ver: "60"},
 {vid: "Decreased ductility", mk: "Magnetic control", ver: "60"},
 {vid: "Decreased ductility", mk: "Eddy current control", ver: "40"},
 {vid: "Decreased ductility", mk: "Metallographic research", ver: "40"},
 {vid: "Decreased ductility", mk: "Visual measurement control", ver: "20"}
];

To plot a grouped bar chart graph using D3, I need to transform the data into the following array of objects:对于 plot 使用 D3 的分组条形图,我需要将数据转换为以下对象数组:

data = [
  {State: "General uneven corrosion", Visual measurement control: 95, Ultrasonic thickness measurement: 95, Ultrasonic flaw detection: 80, Radiographic control: 80, Eddy current control: 60, Magnetic control: 20, Metallographic research: 20, Mechanical property studies: 0, Coercimetry: 0, Acoustic emission control: 0},
  {State: "Increase in strength characteristics", Visual measurement control: 0, Ultrasonic thickness measurement: 0, Ultrasonic flaw detection: 0, Radiographic control: 0, Eddy current control: 0, Magnetic control: 0, Metallographic research: 40, Mechanical property studies: 80, Coercimetry: 40, Acoustic emission control: 0},
  {State: "Increase in hardness", Visual measurement control: 0, Ultrasonic thickness measurement: 0, Ultrasonic flaw detection: 0, Radiographic control: 0, Eddy current control: 0, Magnetic control: 0, Metallographic research: 40, Mechanical property studies: 95, Coercimetry: 60, Acoustic emission control: 0},
  {State: "Decreased ductility", Visual measurement control: 20, Ultrasonic thickness measurement: 0, Ultrasonic flaw detection: 0, Radiographic control: 0, Eddy current control: 40, Magnetic control: 60, Metallographic research: 40, Mechanical property studies: 60, Coercimetry: 60, Acoustic emission control: 60}, 
  columns: ["State", "Visual measurement control", "Ultrasonic thickness measurement", "Ultrasonic flaw detection", "Radiographic control", "Eddy current control", "Magnetic control", "Metallographic research", "Mechanical property studies", "Coercimetry", "Acoustic emission control"]
  y: "Detectability"
];

This is where the every State takes on a unique value of vid and every object of final data array contains set of mk .这是每个State具有唯一值vid并且最终数据数组的每个 object 包含一组mk的地方。 For each object, the order of mk in set is the same, and the value of each mk corresponds to the initial dataSet, or takes 0 if there is no data for this species.对于每一个object, mk在set中的顺序是一样的,每一个mk的值对应于初始dataSet,如果没有该物种的数据则取0。

I can get arrays of unique values of a particular key,我可以获得特定键的唯一值的 arrays,

let allMk = []
dataSet.forEach(item => {
     allMk.push(item.mk)
});
let allMkUni = new Set (allMk)

but I do not understand how to correctly convert objects to the desired form.但我不明白如何正确地将对象转换为所需的形式。 Help please.请帮忙。 Thanks谢谢

Probably easiest to reduce() to an intermediate object, and then map() the entries to an array:可能最简单的方法是reduce()到中间 object,然后将条目map()到数组:

 const data = [ {vid: "General uneven corrosion", mk: "Visual measurement control", ver: "95"}, {vid: "General uneven corrosion", mk: "Ultrasonic thickness measurement", ver: "95"}, {vid: "General uneven corrosion", mk: "Ultrasonic flaw detection", ver: "80"}, {vid: "General uneven corrosion", mk: "Radiographic control", ver: "80"}, {vid: "General uneven corrosion", mk: "Eddy current control", ver: "60"}, {vid: "General uneven corrosion", mk: "Magnetic control", ver: "20"}, {vid: "General uneven corrosion", mk: "Metallographic research", ver: "20"}, {vid: "Increase in strength characteristics", mk: "Mechanical property studies", ver: "80"}, {vid: "Increase in strength characteristics", mk: "Coercimetry", ver: "40"}, {vid: "Increase in strength characteristics", mk: "Metallographic research", ver: "40"}, {vid: "Increase in hardness", mk: "Mechanical property studies", ver: "95"}, {vid: "Increase in hardness", mk: "Coercimetry", ver: "60"}, {vid: "Increase in hardness", mk: "Metallographic research", ver: "40"}, {vid: "Decreased ductility", mk: "Coercimetry", ver: "60"}, {vid: "Decreased ductility", mk: "Mechanical property studies", ver: "60"}, {vid: "Decreased ductility", mk: "Acoustic emission control", ver: "60"}, {vid: "Decreased ductility", mk: "Magnetic control", ver: "60"}, {vid: "Decreased ductility", mk: "Eddy current control", ver: "40"}, {vid: "Decreased ductility", mk: "Metallographic research", ver: "40"}, {vid: "Decreased ductility", mk: "Visual measurement control", ver: "20"} ]; const result = Object.entries(data.reduce((a, {vid, mk, ver}) => { a[vid] = a[vid] || {}; a[vid][mk] = ver; return a; }, {})).map(([k, v]) => ({State: k, ...v})); console.log(result)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将一种形式的JSON数组数据转换为另一种形式? - Converting the one form of JSON Array Data to another? 如何在 TypeScript 中的对象数组中将数据从一个对象数组获取到另一个对象数组中 - How to Get Data From One Array of Objects into another in Array of Objects in TypeScript 在另一个对象数组中转换一个对象数组? - Converting an array of objects in another array of objects? 将对象从一个数组推到另一个数组 - pushing objects from one array to another 将属性从一个对象数组添加到另一个对象 - Add properties from one array of objects to another 将对象数组从一个函数传递到另一个函数 - Passing Array of Objects from one function to another 检查一个对象数组的字段是否从另一个对象数组中丢失 - Check if field of one array of objects is missing from another array of objects 如何从一个对象数组中获取值到另一个对象数组中 - How to get values from one array of objects into another array of objects 根据 Javascript 中另一个数组的数据重新排序一个对象数组(React) - Re-ordering one array of objects based on data from another array in Javascript (React) javascript:将数据从一个对象数组复制到另一个具有现有数据的对象会产生错误的结果 - javascript : copying data from one array of objects to another which has existing data gives incorrect result
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM