我如何转换这样的对象
{
"ID_PROC_GD": "1",
"DT_INI": "2018-06-06",
"CD_GD": "1",
"DT_INI_GD": "2018-05-28",
...
}
对此
[
{
"name": "DT_INI",
"value": "2018-06-06"
},
...
]
我正在使用lodash 3.10.1
答案 0 :(得分:1)
map始终返回数组类型,并且您可以在迭代器中同时使用value和key:
_.map(obj, (value, name) => ({name, value}))
const obj = {
"ID_PROC_GD": "1",
"DT_INI": "2018-06-06",
"CD_GD": "1",
"DT_INI_GD": "2018-05-28",
}
const result = _.map(obj, (value, name) => ({
name,
value
}))
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.min.js"></script>
答案 1 :(得分:1)
您可以使用Object.entries()
和array#map
。
let data = { "ID_PROC_GD": "1", "DT_INI": "2018-06-06", "CD_GD": "1", "DT_INI_GD": "2018-05-28"},
result = Object.entries(data).map(([name, value]) => ({name, value}));
console.log(result);
您可以使用map
let data = { "ID_PROC_GD": "1", "DT_INI": "2018-06-06", "CD_GD": "1", "DT_INI_GD": "2018-05-28"},
result = _.map(data, (value, name) => ({name, value}));
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.min.js"></script>
答案 2 :(得分:0)
let obj = {
"ID_PROC_GD": "1",
"DT_INI": "2018-06-06",
"CD_GD": "1",
"DT_INI_GD": "2018-05-28"
}
function getNameValObj(ele) {
return {name: ele, value:obj[ele]};
}
let objList = _.flatMap(Object.keys(obj), getNameValObj);
答案 3 :(得分:0)
您可以尝试以下方法:
let obj={
"ID_PROC_GD": "1",
"DT_INI": "2018-06-06",
"CD_GD": "1",
"DT_INI_GD": "2018-05-28",
}
_.map(obj,(item,index)=>({
['name']:index,
['value']:item
}))