根据某些条件,从对象和键的数组构建不同的数组结构

时间:2019-05-28 10:13:04

标签: javascript

我有下面的对象和键值作为对象的对象数组。

let results = [
  {
    "id": "yqweyqweu213",
    "number": "123456127890",
    "name": "test 1",
    "group": 'Medium',
    "note": 'Some key notes',
    "status": "Enabled"
  },
  {
    "id": "uqweyewueyyuqwe",
    "number": "173276123612",
    "name": "test 2",
    "area": 'Delhi',
    "note": 'Some key orginal notes',
    "status": 'Disabled',
    "group": 'Medium',
    "level": 'High',
  },
  {
    "id": "kasjdashdkaskjd",
    "number": "128737812381723",
    "name": "test 3",
    "note": 'Some key orginal sample notes',
    "status": 'Enabled',
    "group": 'Low',
    "terminal": 1,
    "area": 'Mumbai'
  }
] 


let fields = {
  "keys": {
    "name": {
      "label": "User Name"
    },
    "number": {
      "label": "User Number"
    },
    "terminal": {
      "label": "Current User Terminal"
    },
    "status": {
      "label": "Active Status"
    },
    "area": {
      "label": "User Specific Area"
    },
    "note": {
      "label": "Notes"
    },
    "group": {
      "label": "Group - User"
    },
    "level": {
      "label": "Level - User"
    }
  }
}

const keysToBeinFlattened = ['name', 'number']

const orderOfFields = ['area', 'terminal', 'note', 'status', 'group', 'level']

const keysToBeMerged = [['note', 'status', seperator: [" ", ","]], ['group', 'level', seperator: [" ",   "(", ")"]]]

我需要获得以下结构,keysToBeinFlattened中的值应位于 展平的结构,如果没有像“ id”这样的键,则不会在 keysToBeinFlattened但也应该在每个对象中展平,因为我 没有ID标签。因此它可以在结果中使用相同的键

现在有名称和数字,因此应该带有相应密钥的标签 并具有相应的值

之后,我需要创建一个基于orderOfFields的fields数组 数组和keysToBeMerged,因此对于注释和状态,值应为逗号 分开使用,对于用户和级别,应在括号中为“级别”。

任何帮助表示赞赏。看到期望的结果

const expectedResult = [{
  id: "yqweyqweu213",
  "User Number": "123456127890", 
  "User Name": "test 1",
  fields: [
    {key: 'User Specific Area', value: null},
    {key: 'Current User Terminal', value: null},
    {key: 'Notes and Active Status', value: 'Some key notes, Enabled'},
    {key: 'Group - User and Level - User', value: 'Medium'}
  ] 
},
{
  id: "uqweyewueyyuqwe",
  "User Number": "173276123612", 
  "User Name": "test 2",
  fields: [
    {key: 'User Specific Area', value: "Delhi"},
    {key: 'Current User Terminal', value: null},
    {key: 'Notes and Active Status', value: 'Some key orginal notes, Disabled'},
    {key: 'Group - User and Level - User', value: 'Medium (High)'}
  ] 
},
{
  id: "kasjdashdkaskjd",
  "User Number": "128737812381723", 
  "User Name": "test 3",
  fields: [
    {key: 'User Specific Area', value: "Mumbai"},
    {key: 'Current User Terminal', value: 1},
    {key: 'Notes and Active Status', value: 'Some key orginal sample notes, Enabled'},
    {key: 'Group - User and Level - User', value: '(Low)'}
    ] 
}]

我已经尝试了以下代码段

let templates = Object.keys(fields.keys).map(
  (key) => ({ [key]: null }),
);

let data = results.map(o =>
  Object.assign(
    {},
    ...templates,
    ...Object.entries(o).map(([key, value]) => ({
      [key]: value,
    })),
  ),
);


let changedOne = data.map(o => {
  let obj = {fields: []}
  let kv = Object.entries(o)
  kv.forEach(([k,v]) => {
   if(orderOfFields.includes(k)){
     obj['fields'].push({key: k, value: v})
   }else{
    obj[k] = v
   }
  })
  return obj 
})

我要实现的jsx模板是

const App = ({data}) => {
  return (
   data.length && data.map(obj => {
     return(
       <>
        <p>`${obj['User Name']} ${obj['User Number']}`</p>
        <button 
          onClick={() => obj.id}
        >Click</button>
        {
          obj.fields.map(o => {
            return (
              <>
              <p>${o.key}</p>
              <p>${o.value}</p>
              </>
            )
          })
        }
       </>
     )
   })
 )
}

0 个答案:

没有答案