2

based on my requiremrnt in my project i am getting the below array of json as input but i need to convert that into a particular format of array of json object.

[
  {
    "accident_description": "bike accident",
    "reported_by": "john",

  },
   {
    "accident_description": "car accident",
    "reported_by": "sam",

  }
]

output>>>

  "fields": [
    {
      "title": "accident_description",
      "values": "bike accident"
      "type": "generic",

    },
    {
      "title": "reported_by",
      "values": "john",
      "type": "generic",

    },
    {
      "title": "accident_description",
      "values": "car accident"
      "type": "generic",

    },
    {
      "title": "reported_by",
      "values": "sam",
      "type": "generic",

    },
  ]
2
  • 1
    Can you show what you have tried so far Commented Apr 17, 2019 at 12:57
  • 1
    to answer the question you deleted (which wasn't really a duplicate of this, just the next step in your code) ... arr.flatMap(o=>Object.entries(o).flatMap(([k,v],i)=>[{title:k,values:v,type:'generic'}].concat(i%2?[{title:'title1',values:'value1',type:'type1'},{title:'title2',values:'value2',type:'type2'}]:[]))); Commented Apr 22, 2019 at 13:32

5 Answers 5

2

You can use the reduce function to get the desired output :-

[
  {
    "accident_description": "bike accident",
    "reported_by": "john",

  },
   {
    "accident_description": "car accident",
    "reported_by": "sam",

  }
].reduce((arr,item) => {
    for(let key in item){
        arr.push({
            "title" : key,
            "values" : item[key],
            "type"  : "generic"
        });
    }
    return arr;

},[]);
Sign up to request clarification or add additional context in comments.

Comments

2

You could map the Object.entires of each object like this:

const input=[{"accident_description":"bike accident","reported_by":"john",},{"accident_description":"car accident","reported_by":"sam",}],
      type = "generic";

const output = input.flatMap(Object.entries)
                    .map(([title, values]) => ({ title, values, type }))
     
console.log(output)

Comments

1

you can use flatMap() and map().

  • Inside flatMap() get the entries of object using Object.entires()
  • Then apply map() on the entries and return an object.

const arr = [ { "accident_description": "bike accident", "reported_by": "john", }, { "accident_description": "car accident", "reported_by": "sam", } ];

let res = arr.flatMap(x => (Object.entries(x).map(([k,v]) => ({title:k,values:v,type:"generic"}))));

console.log(res)

Comments

1

You can use flatMap to loop thru the array. Use Object.entries to convert each object into an array and map to return the desired object.

let arr = [{"accident_description":"bike accident","reported_by":"john"},{"accident_description":"car accident","reported_by":"sam"}];

let result = arr.flatMap(o => Object.entries(o).map(([v, k]) => ({"title": v,"values": k,"type": "generic"})))

console.log(result);

...Or you can also use concat instead of flatMap

let arr = [{"accident_description":"bike accident","reported_by":"john"},{"accident_description":"car accident","reported_by":"sam"}];

let result = [].concat(...arr.flatMap(o => Object.entries(o).map(([v, k]) => ({"title": v,"values": k,"type": "generic"}))));

console.log(result);

Comments

-1

You can use flatMap and reduce to achieve what you want to do

flatMap instead of map will create flat array from looping over your input. i.e. your first obj from input array will become two obj in output array. So insted of merging the two objects in one array flatMap will make it flat i.e. two separate entries. Then use reduce and concat to format data from one obj.

const input = [{
    "accident_description": "bike accident",
    "reported_by": "john",

  },
  {
    "accident_description": "car accident",
    "reported_by": "sam",

  }
]

const out = input.flatMap(obj =>
  Object.keys(obj)
  .reduce((acc, cur) =>
    acc.concat({
      title: cur,
      values: obj[cur],
      type: "dynamic"
    }), [])
)

console.log(out)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.