Javascript对象转换

时间:2019-07-02 08:41:52

标签: javascript node.js transformation

我正在使用地图转换JSON。我得到的不是数组,而是单个数据。 如下所示的摘录。

我正在尝试实现以下JSON

{
  "data": {
    "test1": {
        "abc": {
            "size": "big",
            "id": "1"
        }
    },
    "test2": {
        "abc": {
            "size": "small",
            "id": "2"
        }
    }
  }
}

但是要遵循JSON

{
"data": [{
    "test1": {
        "abc": {
            "size": "big",
            "id": "1"
        }
    }
}, {
    "test2": {
        "abc": {
            "size": "big",
            "id": "2"
        }
    }
}]

}

这是我的代码。

const test = [{ id: 'ddec9c7f-95aa-4d07-a45a-dcdea96309a9',
  ad_id: 'test1',
  country: 'ID',
  abc: { size: 'big', id: '1' },
},
{ id: 'ddec9c7f-95aa-4d07-a45a-dcdea96309a9',
  ad_id: 'test2',
  country: 'ID',
  abc: { size: 'small', id: '2' },
},
];
const transformedTest = test.map(result =>
  ({ [result.ad_id]: { abc: result.abc } }));

const data = { data: transformedTest };
console.log(JSON.stringify(data));

任何帮助将不胜感激

5 个答案:

答案 0 :(得分:6)

尝试一下:

const test =  [
  {
    "id": "ddec9c7f-95aa-4d07-a45a-dcdea96309a9",
    "ad_id": "test1",
    "country": "ID",
    "abc": {
      "size": "big",
      "id": "1"
    }
  },
  {
    "id": "ddec9c7f-95aa-4d07-a45a-dcdea96309a9",
    "ad_id": "test2",
    "country": "ID",
    "abc": {
      "size": "big",
      "id": "2"
    }
  }
];
const result  = test.reduce((acc,{ad_id, abc})=> (acc.data[ad_id] = {abc}, acc),{data:{}}) 

console.log(result);

答案 1 :(得分:2)

函数map返回一个数组,而不是一个特定的key-value对象,另一种方法是使用函数reduce构建所需的输出。

const test = [{ id: 'ddec9c7f-95aa-4d07-a45a-dcdea96309a9',  ad_id: 'test1',  country: 'ID',  abc: { size: 'big', id: '1' },},{ id: 'ddec9c7f-95aa-4d07-a45a-dcdea96309a9',  ad_id: 'test2',  country: 'ID',  abc: { size: 'big', id: '2' },}],
      data = { data: test.reduce((a, result) =>
        ({...a, [result.ad_id]: { abc: result.abc } }), Object.create(null))};

console.log(JSON.stringify(data, null, 2));
.as-console-wrapper { min-height: 100%; }

答案 2 :(得分:2)

const transformedTest = test.reduce((pv, result) =>
({...pv, [result.ad_id]: { abc: result.abc } }), {});

答案 3 :(得分:2)

您可以使用Object.fromEntries并映射新的键/值对,并从中获取一个新对象。

const
    test = [{ id: 'ddec9c7f-95aa-4d07-a45a-dcdea96309a9', ad_id: 'test1', country: 'ID', abc: { size: 'big', id: '1' } }, { id: 'ddec9c7f-95aa-4d07-a45a-dcdea96309a9', ad_id: 'test2', country: 'ID', abc: { size: 'small', id: '2' } }];
    transformedTest = Object.fromEntries(test.map(({ ad_id, abc} ) => [ad_id, { abc }]));
    data = { data: transformedTest };

console.log(data);

答案 4 :(得分:2)

使用简单的for...of循环遍历数组,并创建一个data对象。使用Shorthand property names创建abc嵌套:

const test=[{id:'ddec9c7f-95aa-4d07-a45a-dcdea96309a9',ad_id:'test1',country:'ID',abc:{size:'big',id:'1'},},{id:'ddec9c7f-95aa-4d07-a45a-dcdea96309a9',ad_id:'test2',country:'ID',abc:{size:'small',id:'2'}}]
const data = {}

for(const { ad_id, abc } of test) {
  data[ad_id] = { abc }
}

console.log({ data })
.as-console-wrapper { min-height: 100%; }