简体   繁体   English

如何将一种 object 格式转换为另一种 object 格式

[英]How to convert one object format to another object format

This is the data I am receiving from the database based on the input values by the user.这是我根据用户输入值从数据库接收的数据。 Like REX,JC2.像REX,JC2。

Main Data = [
  { 'SerNoPfx1': 'REX', 'SerNoPfx2': 'REX', 'Percentage': 100 },
  { 'SerNoPfx1': 'JC2', 'SerNoPfx2': 'JC2', 'Percentage': 100 },
  { 'SerNoPfx1': 'JC2', 'SerNoPfx2': 'REX', 'Percentage': 52.93 },
  { 'SerNoPfx1': 'REX', 'SerNoPfx2': 'JC2', 'Percentage': 52.93 },
];

needs to be converted to需要转换为

data = [
  { "REX": "100", "JC2": "52" }, 
  { "REX": "0", "JC2": "100" },
];

{ 'SerNoPfx1': 'JC2', 'SerNoPfx2': 'REX', 'Percentage': 52.93 },{ 'SerNoPfx1': 'REX', 'SerNoPfx2': 'JC2', 'Percentage': 52.93 },

I have to take only one line because this is the matching percentage of REX and JC2.我只需要取一行,因为这是 REX 和 JC2 的匹配百分比。

REX,JC2 are the input values by the user. REX,JC2 是用户输入的值。 It is always dynamic.它总是动态的。 I need to convert this using JavaScript.我需要使用 JavaScript 进行转换。

It looks like you want to get a filtered subset of the original array of objects.看起来您想要获取原始对象数组的过滤子集。 I. e. IE。 the objects matching both given strings in their SerNoPfx1 and SerNoPfx2 properties (regardless of the order).在其SerNoPfx1SerNoPfx2属性中匹配给定字符串的对象(无论顺序如何)。

 const data = [ { 'SerNoPfx1': 'REX', 'SerNoPfx2': 'REX', 'Percentage': 100 }, { 'SerNoPfx1': 'JC2', 'SerNoPfx2': 'JC2', 'Percentage': 100 }, { 'SerNoPfx1': 'JC2', 'SerNoPfx2': 'REX', 'Percentage': 52.93 }, { 'SerNoPfx1': 'REX', 'SerNoPfx2': 'JC2', 'Percentage': 52.93 } ]; const str="REX,JC2"; const fltStr=str.split(",").sort().join(","); const res=data.filter(d=>[d.SerNoPfx1,d.SerNoPfx2].sort().join(",")===fltStr); console.log(res);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM