user11229655
user11229655

Reputation: 248

Javascript Object Tranformation

I am using the map to transforming the JSON. Instead of Single object, I am getting an Array of data. Snipt Shown Below.

I am trying to achieve following JSON

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

But getting following JSON

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

}

Here is my code.

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));

Any help will be appreciated

Upvotes: 2

Views: 85

Answers (5)

adiga
adiga

Reputation: 35202

Loop through the array using a simple for...of loop and create a data object. Use Shorthand property names to create the abc nesting:

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%; }

Upvotes: 2

Nina Scholz
Nina Scholz

Reputation: 386540

You could take Object.fromEntries and map new key/value pairs and get a new object from it.

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);

Upvotes: 2

Ghoul Ahmed
Ghoul Ahmed

Reputation: 4806

Try this:

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);

Upvotes: 3

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

Upvotes: 2

Ele
Ele

Reputation: 33726

The function map returns an array rather than a specific key-value object, an alternative is using the function reduce to build the desired output.

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%; }

Upvotes: 1

Related Questions