简体   繁体   English

用Javascript转换对象格式

[英]Converting object format in Javascript

I have an input array of objects, which each object has the following format: 我有一个对象输入数组,每个对象都具有以下格式:

{
 titleID: string,
 titleName: string,
 1af23_red: number,
 45ua6_blue: number
}

What I know is that: 我所知道的是:

  1. In every object, there will be always the keys titleID and titleName and then I will have several keys that have the format number_string . 在每个对象中, 总会有键titleIDtitleName ,然后我将有几个键的格式为number_string

  2. The titleID and titleName values will be different among the different objects 不同对象之间的titleIDtitleName值将有所不同

  3. The rest of the keys (1af23_red, 45ua6_blue, etc) will be the same in all objects and they will all have the same format 'id_name' So, if the first object has 1af23_red and 45ua6_blue as keys, all the rest will also have just those keys. 其余所有键(1af23_red,45ua6_blue等)在所有对象中都是相同的,并且它们的格式均为“ id_name”,因此,如果第一个对象具有1af23_red和45ua6_blue作为键,则其余所有键这些键。

The type of array that I want back has the following format: 我要返回的数组类型具有以下格式:

{
  color: {
    id
    name
  },
  data: Array<
  {
    title: {
      id
      name
    },
    rating
  }
  >
}

So, example of input: 因此,输入示例:

[ 
  { 
    titleId: 'a',
    titleName: 'atitle',
    '1af_red': 50
    'ba2_blue': 40
  },
  {
    titleId: 'b',
    titleName: 'btitle',
    '1af_red': 30
    'ba2_blue': null
  },
  {
    titleId: 'c',
    titleName: 'ctitle',
    '1af_red': null
    'ba2_blue': 10
  }
]

I would expect back: 我希望回来:

[
  {
    color: {
      id: '1af',
      name: 'red'
    },
    data: [
      {
        title: {
          id: 'a',
          name: 'atitle',
        },
        rating: 50
      }, 
      {
        title: {
          id: 'b',
          name: 'btitle',
        },
        rating: 30
      },
      {
        title: {
          id: 'c',
          name: 'ctitle',
        },
        rating: null
      }
    ]
  },
  {
    color: {
      id: 'ba2',
      name: 'blue'
    },
    data: [
      {
        title: {
          id: 'a',
          name: 'atitle',
        },
        rating: 40
      }, 
      {
        title: {
          id: 'b',
          name: 'btitle',
        },
        rating: null
      },
      {
        title: {
          id: 'c',
          name: 'ctitle',
        },
        rating: 10
      }
    ]
  }
]

I have tried doing this conversion with map and reduce but I am stuck. 我尝试使用map和reduce进行这种转换,但是我遇到了麻烦。 Is there an easy way to accomplish this? 有没有简单的方法可以做到这一点?

Here you go. 干得好。

Brief logic : Getting all the keys from the object at 0th index from data array. 简要逻辑:从数据数组的第0个索引处获取对象的所有键。 Looping over the keys, if key contains '_', pick the key, break it to form id and name pair, then map over all the data objects, get the score for that key and append it to the object with id and name values. 遍历键,如果键包含“ _”,则选择键,将其断开以形成ID和名称对,然后映射所有数据对象,获取该键的分数并将其添加到具有ID和名称值的对象中。 Finally append this object to result array. 最后将此对象附加到结果数组。 Doing this for all the keys which contains '_'. 对所有包含“ _”的键执行此操作。

 const data = [ { titleId: 'a', titleName: 'atitle', '1af_red': 50, 'ba2_blue': 40 }, { titleId: 'b', titleName: 'btitle', '1af_red': 30, 'ba2_blue': null }, { titleId: 'c', titleName: 'ctitle', '1af_red': null, 'ba2_blue': 10 } ]; const keys = Object.keys(data[0]); const result = [] keys.map(key=> { if(key.indexOf('_')!==-1){ const item = {} const keyData = key.split('_') item.color = { id : keyData[0], name : keyData[1] } item.data = [] data.map(obj => { const newObj = {} newObj.title = { id : obj.titleId, name : obj.titleName } newObj.rating = obj[key]; item.data.push(newObj); }); result.push(item); } }); console.log(result); 

You could try that 你可以试试看

 let elements = [ { titleId: 'a', titleName: 'atitle', '1af_red': 50, 'ba2_blue': 40 }, { titleId: 'b', titleName: 'btitle', '1af_red': 30, 'ba2_blue': null }, { titleId: 'c', titleName: 'ctitle', '1af_red': null, 'ba2_blue': 10 } ] let colors = [] let result = [] elements.forEach(currElem => { for(let attr in currElem){ if(attr != "titleId" && attr != "titleName"){ let color = attr.split("_") if(!colors.some(currColor => {return currColor == color[1]})){ colors.push({ "id": color[0], "name": color[1] }) } } } }) colors.forEach(currColor => { result.push({ "color" : currColor, "data": [] }) elements.forEach(currElement => { for(let attr in currElement){ let color = [] if(attr != "titleId" && attr != "titleName"){ color = attr.split("_") if(color[1] == currColor.name){ for(let i=0; i<result.length;i++){ if(result[i].color.name == color[1]){ result[i].data.push({ "title" : { "id": currElement.titleId, "name": currElement.titleName }, "rating":currElement[attr] }) break } } } } } }) }) console.log(result) 

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

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