1

I have an object

{
 "input":{
  "id": "7879",
  "inputType": "9876",
  "streamName": "870"
  },
 "transformation":{
   "id": "7",
   "dependencies": [
          "8i"
    ],
   "dropColumns": "hkj",
   "processor": "hgf"
 },
 "output": {
   "id": "v",
   "saveMode": "uyt",
   "dependentIds": [
        "h"
    ],
   "outPartition":[
        "hg"
    ]
 }
}

Basically every value leaving the key I have to put it in an array. So input, transformation, output values(which are objects) should be placed inside array.

Expected Output:

{
 "input": [
  {
  "id": "7879",
  "inputType": "9876",
  "streamName": "870"
  }
  ],
 "transformation":[
   {
   "id": "7",
   "dependencies": [
          "8i"
    ],
   "dropColumns": "hkj",
   "processor": "hgf"
 }
 ],
 "output":[
   {
   "id": "v",
   "saveMode": "uyt",
   "dependentIds": [
        "h"
    ],
   "outPartition":[
        "hg"
    ]
 }
 ]
}

I tried iterating using the for in loop but was not able to achieve the expected output how should I place the values(object) inside array

var arr = [];
for(key in obj){
  arr.push(Object.assign(obj[key],{name: key}))
}
2
  • Why arr? Why Object.assign()? Why {name: key}? Commented Mar 11, 2021 at 14:32
  • Why transform this structure at all? It is perfectly usable for any purpose. Commented Mar 11, 2021 at 14:34

3 Answers 3

3

You mean this (although not sure why you would need this)?

const input = {
 "input":{
  "id": "7879",
  "inputType": "9876",
  "streamName": "870"
  },
 "transformation":{
   "id": "7",
   "dependencies": [
          "8i"
    ],
   "dropColumns": "hkj",
   "processor": "hgf"
 },
 "output": {
   "id": "v",
   "saveMode": "uyt",
   "dependentIds": [
        "h"
    ],
   "outPartition":[
        "hg"
    ]
 }
}
Object.keys(input).forEach(key => input[key] = [input[key]] )
console.log(input)

Sign up to request clarification or add additional context in comments.

Comments

0

According to the output of the object you provided, a simple implementation of looping the elements and creating the keys as arrays can be:

for(key in obj){
  obj[key] = [obj[key]];
}

Comments

0

You could rebuild it using the reduce function if you want to do it immutable:

The reduce() method executes a reducer function (that you provide) on each element of the array, resulting in single output value.

More info see: MDN Webdocs - reduce()

const data = {
 "input":{
  "id": "7879",
  "inputType": "9876",
  "streamName": "870"
  },
 "transformation":{
   "id": "7",
   "dependencies": ["8i"],
   "dropColumns": "hkj",
   "processor": "hgf"
 },
 "output": {
   "id": "v",
   "saveMode": "uyt",
   "dependentIds": ["h"],
   "outPartition":["hg"]
 }
};

const customFormat = (data) => Object.keys(data).reduce((object, key) => {
    object[key] = [data[key]];
  return object
}, {});

console.log(customFormat(data));

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.