0

I had an issue earlier and received some help with it earlier but now i'm having an issue updating/adding a link to earlier issue.

I have a slightly different setup at this point because i couldn't figure out how to update my data. At this point. I need to get the data from my Array A (filepath) into my list of objects as another property in my sub object with a key of "-i". I feel like i should be able to iterate through the array and add one of these values to my object but i've tried using the reduce and loop features but haven't been able to get my desired output, if there's a easier way from starting with my last post issue i'd be happy to go back to that state.

//Desired ouput
{
      process0000x0000: {-i:"D:\\Code\\UnitTest\\Tests\\Run_01_TEMP\\0000x0000.png, tr: 16, tc: 16, ofr: 16, ofc: 16, outfile: 'D:\\Code\\Process\\1' },
      process0000x0001: {-i:"D:\\Code\\UnitTest\\Tests\\Run_01_TEMP\\0000x0001.png", tr: 16, tc: 16, ofr: 16, ofc: 16, outfile: 'D:\\Code\\Process\\1' },
      process0000x0002: {-i:"D:\\Code\\UnitTest\\Tests\\Run_01_TEMP\\0000x0002.png", tr: 16, tc: 16, ofr: 16, ofc: 16, outfile: 'D:\\Code\\Process\\1' }
    }

//Array A
    "outputParameters": [
    {
        "name": "0000x0000",
        "filepath": "D:\\Code\\UnitTest\\Tests\\Run_01_TEMP\\0000x0000.png"
    },
    {
        "name": "0000x0001",
        "filepath": "D:\\Code\\UnitTest\\Tests\\Run_01_TEMP\\0000x0001.png"
    },
    {
        "name": "0000x0002",
        "filepath": "D:\\Code\\UnitTest\\Tests\\Run_01_TEMP\\0000x0002.png"
    }]



 // current 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' }
}

UPDATE: so i added the below but i'm still receiving the last "filepath" for each entry in my consoleparamscompiled data. I tried your way and the way i have shown below but with the same results.

  //results
 },
  process13x21: {
    '-i': 'D:\\Code\\UnitTest\\ConsoleApp\\1\\13x23.png',
    '-tr': 16,
    '-tc': 16,
    '-ofr': 16,
    '-ofc': 16,
    '-outfile': '"D:\\Code\\UnitTest\\ConsoleApp\\Process\\1"'
  },
  process13x22: {
    '-i': 'D:\\Code\\UnitTest\\ConsoleApp\\1\\13x23.png',
    '-tr': 16,
    '-tc': 16,
    '-ofr': 16,
    '-ofc': 16,
    '-outfile': '"D:\\Code\\UnitTest\\ConsoleApp\\Process\\1"'
  },
  process13x23: {
    '-i': 'D:\\Code\\UnitTest\\ConsoleApp\\1\\13x23.png',
    '-tr': 16,
    '-tc': 16,
    '-ofr': 16,
    '-ofc': 16,
    '-outfile': '"D:\\Code\\UnitTest\\ConsoleApp\\Process\\1"'
  }
}
// ADDED CODE
// loop through each data we want to add and add a property.
   ` consoleOutputParamsOBJ.forEach((obj) => {
      var processname = dynamicTaskNameBaseOBJ + obj.name;
      console.log(processname);
      //taskparamscompiled.processname['-i'] = obj.filepath;
      taskparamscompiled[processname]['-i'] = obj.filepath;

      // console.log(dynamicTaskNameBaseOBJ + obj.name);
    });
    console.log(taskparamscompiled);`

1 Answer 1

0

I'm not 100% sure I understand your question, but here goes.

You can access an objects property by a string by using [] instead of .

E.g.

let x = {name: "chris"};

x.name = 'Sam';

x['name'] = 'Ben';

The above will both change the name property

Here is a code example with the data you provided.

let data = [{
  "name": "0000x0000",
  "filepath": "D:\\Code\\UnitTest\\Tests\\Run_01_TEMP\\0000x0000.png"
}, {
  "name": "0000x0001",
  "filepath": "D:\\Code\\UnitTest\\Tests\\Run_01_TEMP\\0000x0001.png"
}, {
  "name": "0000x0002",
  "filepath": "D:\\Code\\UnitTest\\Tests\\Run_01_TEMP\\0000x0002.png"
}];

let result = {
  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'
  }
}

// loop through each data we want to add and add a property.
data.forEach(obj => {
  result[`process${obj.name}`]["-i"] = obj.filepath
})

console.log(result)

And the output is :

{
  process0000x0000: {
    -i: "D:\Code\UnitTest\Tests\Run_01_TEMP\0000x0000.png",
    ofc: 16,
    ofr: 16,
    outfile: "D:\Code\Process\1",
    tc: 16,
    tr: 16
  },
  process0000x0001: {
    -i: "D:\Code\UnitTest\Tests\Run_01_TEMP\0000x0001.png",
    ofc: 16,
    ofr: 16,
    outfile: "D:\Code\Process\1",
    tc: 16,
    tr: 16
  },
  process0000x0002: {
    -i: "D:\Code\UnitTest\Tests\Run_01_TEMP\0000x0002.png",
    ofc: 16,
    ofr: 16,
    outfile: "D:\Code\Process\1",
    tc: 16,
    tr: 16
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Sorry I can't work out what the issue is with that comment, could you add more data to your question? Maybe some data examples and the output of the comment above?
I'm sorry, i had added that to the comment section on accident. I updated my original post above at the bottom of the post. Not really familiar with the StackOverflow edicate
I created another post if it helps, stackoverflow.com/questions/66230508/…

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.