0

My desired output is at the bottom of this post, I want to remove the array and put a list of objects inside an object.

I'm sharing my map function because i'm hopeful that there's a method inside there that can get my desired input and i'm just not doing it correctly.

If what i'm doing doesn't make sense i would love a better method of manipulating this data.

Starting Data(./updatepayloadTEMP.json)

{
 "outputparameters": [
    {
        "name": "0000x0000",
        "filepath": "D:\\Code\\ImageTiling\\6\\0000x0000.png"
    },
    {
        "name": "0000x0001",
        "filepath": "D:\\Code\\ImageTiling\\6\\0000x0001.png"
    },
    {
        "name": "0000x0002",
        "filepath": "D:\\Code\\ImageTiling\\6\\0000x0002.png"
    }
 ]
}

Variables

 let UpdatedTaskOutput = fs.readFileSync('./updatepayloadTEMP.json'); 
 let Updatedtaskoutputjson = JSON.parse(UpdatedTaskOutput); 
 var dynamictaskdetails = Updatedtaskoutputjson.outputparameters;

MAP func

   var taskparamscompiled = dynamictaskdetails.map(function (elem) {
   taskname = tasknamefromworkflowdef + elem.name;
   taskparms = taskparamsobj;
   return {
    [taskname]: taskparms,
   };
  });

What i'm currently getting for taskparamscompiled

 [
 {
   process0000x0000: {
    tr: 16,
    tc: 16,
    ofr: 16,
    ofc: 16,
    outfile: '"D:\\Code\\Process\\1"',
   },
  },
{
 process0000x0001: {
   tr: 16,
   tc: 16,
   ofr: 16,
   ofc: 16,
   outfile: '"D:\\Code\\Process\\1"',
  },
},
{
 process0000x0002: {
   tr: 16,
   tc: 16,
   ofr: 16,
   ofc: 16,
   outfile: '"D:\\Code\\Process\\1"',
 },
},
];

What I want

 {
   "process0000x0000": {
   "tr": 16,
   "tc": 16,
   "ofr": 16,
   "ofc": 16,
   "outfile": '"D:\\Code\\Process\\1"'
 },

    "process0000x0001": {
   "tr": 16,
   "tc": 16,
   "ofr": 16,
   "ofc": 16,
   "outfile": '"D:\\Code\\Process\\1"'
 },
 "process0000x0002": {
   "tr": 16,
   "tc": 16,
   "ofr": 16,
   "ofc": 16,
   "outfile": '"D:\\Code\\Process\\1"'
 },
}

I missed something, UPDATE: I now need to get the filepath ( "filepath": "D:\\Code\\ImageTiling\\6\\0000x0000.png") into the process object like this

process0000x0000: {
   filepath: "D:\\Code\\ImageTiling\\6\\0000x0000.png" 
    tr: 16,
    tc: 16,
    ofr: 16,
    ofc: 16,
    outfile: '"D:\\Code\\Process\\1"',
   }

1 Answer 1

1

Array.prototype.map() will return an array. You probably want to use Array.prototype.reduce(). https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce

I think this gives you your desired output

const dynamictaskdetails = [
  {
    name: '0000x0000',
    filepath: 'D:\\Code\\ImageTiling\\6\\0000x0000.png'
  },
  {
    name: '0000x0001',
    filepath: 'D:\\Code\\ImageTiling\\6\\0000x0001.png'
  },
  {
    name: '0000x0002',
    filepath: 'D:\\Code\\ImageTiling\\6\\0000x0002.png'
  }
];

const taskparamsobj = {
  tr: 16,
  tc: 16,
  ofr: 16,
  ofc: 16,
  outfile: 'D:\\Code\\Process\\1'
};

const taskparamscompiled = dynamictaskdetails.reduce((accumulator, elem) => {
  const taskname = 'process' + elem.name;
  return {
    ...accumulator,
    [taskname]: taskparamsobj,
  };
}, {});

console.log(taskparamscompiled);

This resulted in the following output:

{
  process0000x0000: { tr: 16, tc: 16, ofr: 16, ofc: 16, outfile: 'D:\\Code\\Process\\1' },
  process0000x0001: { tr: 16, tc: 16, ofr: 16, ofc: 16, outfile: 'D:\\Code\\Process\\1' },
  process0000x0002: { tr: 16, tc: 16, ofr: 16, ofc: 16, outfile: 'D:\\Code\\Process\\1' }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much, i had a feeling i wasn't using Array.prototype.map() correctly
idk if i should reply or ask here but, What if i want to add the filepath from ./updatepayloadTEMP.json to the sub objects with the other properties?
last ditch effort, i made another post with my current status. stackoverflow.com/questions/66230508/… @josh-pospisil

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.