0

how can i convert an object like this

{
  "ID_PROC_GD": "1",
  "DT_INI": "2018-06-06",
  "CD_GD": "1",
  "DT_INI_GD": "2018-05-28",
  ...
}

to this

[
  {
    "name": "DT_INI",
    "value": "2018-06-06"
  },
 ...
]

I am using lodash 3.10.1

Leonel Matias Domingos
  • 1,922
  • 5
  • 29
  • 53
  • 2
    The posted question does not appear to include [any attempt](https://idownvotedbecau.se/noattempt/) at all to solve the problem. StackOverflow expects you to [try to solve your own problem first](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users), as your attempts help us to better understand what you want. Please edit the question to show what you've tried, so as to illustrate a specific roadblock you're running into a [MCVE]. For more information, please see [ask] and take the [tour]. – CertainPerformance Nov 15 '18 at 12:25
  • you should search propely before posting a new question. This answer (of mine) will surely serve your purpose https://stackoverflow.com/questions/24674630/transform-object-to-array-with-lodash/45897196#45897196 – Koushik Chatterjee Nov 15 '18 at 13:14

4 Answers4

1

map always returns array type, and you can use both value and key in iterator:

_.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>
iofjuupasli
  • 3,818
  • 1
  • 16
  • 17
1

You can use Object.entries() and 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);

You can use 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>
Hassan Imam
  • 21,956
  • 5
  • 41
  • 51
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);
Mukund
  • 393
  • 2
  • 7
  • While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find [how to write a good answer](https://stackoverflow.com/help/how-to-answer) very helpful. Please edit your answer - [From Review](https://stackoverflow.com/review/low-quality-posts/21428867) – Nick Nov 16 '18 at 08:22
  • Sure your are right. The explanation to solution is, lowdash _.flatMap method flattens the array of values by running each element in collection thru iteratee and flattening the mapped results. Method return new flattened array as per the return value of the method passed to second argument. – Mukund Nov 17 '18 at 04:36
0

You can try this:

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
}))
Dino
  • 7,779
  • 12
  • 46
  • 85
Akash Singh
  • 547
  • 5
  • 8