繁体   English   中英

用Javascript转换对象格式

[英]Converting object format in Javascript

我有一个对象输入数组,每个对象都具有以下格式:

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

我所知道的是:

  1. 在每个对象中, 总会有键titleIDtitleName ,然后我将有几个键的格式为number_string

  2. 不同对象之间的titleIDtitleName值将有所不同

  3. 其余所有键(1af23_red,45ua6_blue等)在所有对象中都是相同的,并且它们的格式均为“ id_name”,因此,如果第一个对象具有1af23_red和45ua6_blue作为键,则其余所有键这些键。

我要返回的数组类型具有以下格式:

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

因此,输入示例:

[ 
  { 
    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
  }
]

我希望回来:

[
  {
    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
      }
    ]
  }
]

我尝试使用map和reduce进行这种转换,但是我遇到了麻烦。 有没有简单的方法可以做到这一点?

干得好。

简要逻辑:从数据数组的第0个索引处获取对象的所有键。 遍历键,如果键包含“ _”,则选择键,将其断开以形成ID和名称对,然后映射所有数据对象,获取该键的分数并将其添加到具有ID和名称值的对象中。 最后将此对象附加到结果数组。 对所有包含“ _”的键执行此操作。

 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); 

你可以试试看

 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