0

I have this json I need to format it in Typescript or java script. what would be better way to do.

var data = {
"value 1" : [
    {
        type : String,
        Dicription : "abc"
    },
    {
        type : int,
        Dicription : "xyz"
    },
    {
        type : String,
        Dicription : "pqr"
    },
    ]
"value 2" : [
    {
        type : String,
        Dicription : "abc"
    }
    ]
"value 3" : [
    {
        type : String,
        Dicription : "abc"
    },
    {
        type : int,
        Dicription : "xyz"
    }
}

Need Output like this

{
    {
       value : value1,
       type : String,
       Description : "abc"
    },
    {
       value : value1,
       type : int,
       Dicription : "xyz"
    },
    {
       value : value1,
       type : String,
       Dicription : "pqr"
    },
    {
        value : value2,
        type : String,
        Description : "abc"
    },
    {
        value : value3,
        type : String,
        Description : "abc"
    },
    {   value : value3,
        type : int,
        Description : "xyz"
    }
}

I tried

var new = [];
Var values = Object.keys(data)
values.ForEach(Function(value){

new.push({
'value' : value })

});

and iterate it, but could not get desired output. I tried to flatten this but I got objects like {value : value , { type: String ,Description : abc}} What should I do to solve it

3
  • Your input and output do not add up. You have space in each value 1, value 2 etc inputs, and no space in the output example. And you do not explain such a data mutation. Commented Oct 28, 2022 at 11:49
  • I have multiple values in a list, I just need to add one more field. I need to display value1 , data type and description in a table. fo this I thought I should manipulate json and then display. Commented Oct 28, 2022 at 11:55
  • You can not have an object of objects. Please edit your expected output. Commented Oct 28, 2022 at 13:48

2 Answers 2

2

Convert the object to an an array using Object.entries(), and then flat map the entries to an array of objects:

const result = Object.entries(data) // get the entries of the object
  .flatMap(([value, arr]) =>  // map and flatten the sub-arrays
    arr.map(o => ({ // map each sub-array and combine with the value
      value,
      ...o
    }))
  )
Sign up to request clarification or add additional context in comments.

Comments

0

I get the keys and values separately and iterate though first object and create another object and push values in it.

So my solution for the problem is

var keys = Object.keys(data);
var values = Object.values(data);
var length = Object.values(data).length;
keys.ForEach(function (key, index){
values.ForEach(function (obj, i) {
if(index == i){
   var innerValue = Object.values(obj);
   for(i=0 ; i<=innerValue.length; i++)
   {
    new.push({
           'value: key,
           'type': innerValue[i].type,
           'Description': innerValue[i].abc,
           });
    }
}
});
});```


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.