0

I'm trying to loop through the consoleOuputParamsOBJ and update a record in my taskparamscompiled list of objects

Desired Output

    {
    "process00x00": {
        "-i": "D:\\Code\\UnitTest\\ConsoleApp\\1\\00x00.png",
        "-tr": 16,
        "-tc": 16,
        "-ofr": 16,
        "-ofc": 16,
        "-outfile": "\"D:\\Code\\UnitTest\\ConsoleApp\\Process\\1\""
    },
    "process00x01": {
        "-i": "D:\\Code\\UnitTest\\ConsoleApp\\1\\00x01.png",
        "-tr": 16,
        "-tc": 16,
        "-ofr": 16,
        "-ofc": 16,
        "-outfile": "\"D:\\Code\\UnitTest\\ConsoleApp\\Process\\1\""
    },
    "process00x02": {
        "-i": "D:\\Code\\UnitTest\\ConsoleApp\\1\\00x02.png",
        "-tr": 16,
        "-tc": 16,
        "-ofr": 16,
        "-ofc": 16,
        "-outfile": "\"D:\\Code\\UnitTest\\ConsoleApp\\Process\\1\""
    }

What i'm currently doing, the processname seems to be working since it's updating that value but the data it's using to update it with is only the last record from the taskparamscompiled dataset.

CURRENT CODE UPDATE:

var consoleOutputParamsOBJ = [{
    name: '00x00',
    filepath: 'D:\\Code\\UnitTest\\ConsoleApp\\1\\00x00.png'
  },
  {
    name: '00x01',
    filepath: 'D:\\Code\\UnitTest\\ConsoleApp\\1\\00x01.png'
  },
  {
    name: '00x02',
    filepath: 'D:\\Code\\UnitTest\\ConsoleApp\\1\\00x02.png'
  },
  {
    name: '01x00',
    filepath: 'D:\\Code\\UnitTest\\ConsoleApp\\1\\01x00.png'
  },
  {
    name: '01x01',
    filepath: 'D:\\Code\\UnitTest\\ConsoleApp\\1\\01x01.png'
  },
  {
    name: '01x02',
    filepath: 'D:\\Code\\UnitTest\\ConsoleApp\\1\\01x02.png'
  }
]

var taskparamscompiled = {
  haralick_process00x00: {
    '-i': '',
    '-tr': 16,
    '-tc': 16,
    '-ofr': 16,
    '-ofc': 16,
    '-outfile': '"D:\\Code\\UnitTest\\ConsoleApp\\Process\\1"'
  },
  haralick_process00x01: {
    '-i': '',
    '-tr': 16,
    '-tc': 16,
    '-ofr': 16,
    '-ofc': 16,
    '-outfile': '"D:\\Code\\UnitTest\\ConsoleApp\\Process\\1"'
  },
  haralick_process00x02: {
    '-i': '',
    '-tr': 16,
    '-tc': 16,
    '-ofr': 16,
    '-ofc': 16,
    '-outfile': '"D:\\Code\\UnitTest\\ConsoleApp\\Process\\1"'
  },
  haralick_process01x00: {
    '-i': '',
    '-tr': 16,
    '-tc': 16,
    '-ofr': 16,
    '-ofc': 16,
    '-outfile': '"D:\\Code\\UnitTest\\ConsoleApp\\Process\\1"'
  },
  haralick_process01x01: {
    '-i': '',
    '-tr': 16,
    '-tc': 16,
    '-ofr': 16,
    '-ofc': 16,
    '-outfile': '"D:\\Code\\UnitTest\\ConsoleApp\\Process\\1"'
  },
  haralick_process01x02: {
    '-i': '',
    '-tr': 16,
    '-tc': 16,
    '-ofr': 16,
    '-ofc': 16,
    '-outfile': '"D:\\Code\\UnitTest\\ConsoleApp\\Process\\1"'
  }
}
var dynamicTaskParamsBaseOBJ = {
  '-i': '',
  '-tr': 16,
  '-tc': 16,
  '-ofr': 16,
  '-ofc': 16,
  '-outfile': '"D:\\Code\\UnitTest\\ConsoleApp\\HaralickProcess\\1"'
}
var dynamicTaskNameBaseOBJ = 'haralick_process'

var taskparamscompiled = consoleOutputParamsOBJ.reduce(
  (accumulator, elem) => {
    const taskname = dynamicTaskNameBaseOBJ + elem.name;
    return {
      ...accumulator,
      [taskname]: dynamicTaskParamsBaseOBJ,
    };
  }, {}
);


consoleOutputParamsOBJ.forEach((obj) => {
  var processname = dynamicTaskNameBaseOBJ + obj.name;
  filepath = obj.filepath;
  taskparamscompiled[processname]['-i'] = filepath;
});

console.log('consoleOutputParamsOBJ::', consoleOutputParamsOBJ, '  \n taskparamscompiled::', taskparamscompiled);

0

2 Answers 2

1

Your issue is occurring because you are assigning copies of the same object to each of the keys in taskparamscompiled with this line:

[taskname]: dynamicTaskParamsBaseOBJ

You need to copy the object instead, which you can do with Object.assign. You can use that to update the input filename (the -i property) at the same time as you build the taskparamscompiled object:

const consoleOutputParamsOBJ = [{
    name: '00x00',
    filepath: 'D:\\Code\\UnitTest\\ConsoleApp\\1\\00x00.png'
  },
  {
    name: '00x01',
    filepath: 'D:\\Code\\UnitTest\\ConsoleApp\\1\\00x01.png'
  },
  {
    name: '00x02',
    filepath: 'D:\\Code\\UnitTest\\ConsoleApp\\1\\00x02.png'
  },
  {
    name: '01x00',
    filepath: 'D:\\Code\\UnitTest\\ConsoleApp\\1\\01x00.png'
  },
  {
    name: '01x01',
    filepath: 'D:\\Code\\UnitTest\\ConsoleApp\\1\\01x01.png'
  },
  {
    name: '01x02',
    filepath: 'D:\\Code\\UnitTest\\ConsoleApp\\1\\01x02.png'
  }
]

const dynamicTaskParamsBaseOBJ = {
  '-i': '',
  '-tr': 16,
  '-tc': 16,
  '-ofr': 16,
  '-ofc': 16,
  '-outfile': '"D:\\Code\\UnitTest\\ConsoleApp\\HaralickProcess\\1"'
}
const dynamicTaskNameBaseOBJ = 'haralick_process'

const taskparamscompiled = consoleOutputParamsOBJ.reduce(
  (accumulator, elem) => {
    const taskname = dynamicTaskNameBaseOBJ + elem.name;
    return {
      ...accumulator,
      [taskname]: Object.assign({}, dynamicTaskParamsBaseOBJ, { '-i' : elem.filepath })
    };
  }, {}
);


console.log(taskparamscompiled);

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

Comments

1

Here is a minimal version of the code provided in the question. It is working as expected.

const outputParams = [
  {
    name: '00x00',
    filepath: 'D:\\Code\\UnitTest\\ConsoleApp\\1\\00x00.png',
  },
  {
    name: '00x01',
    filepath: 'D:\\Code\\UnitTest\\ConsoleApp\\1\\00x01.png',
  },
  {
    name: '00x02',
    filepath: 'D:\\Code\\UnitTest\\ConsoleApp\\1\\00x02.png',
  },
];

const taskparamscompiled = {
  process00x00: {},
  process00x01: {},
  process00x02: {}
};

const dynamicTaskNameBaseOBJ = 'process';

outputParams.forEach((obj) => {
  var processname = dynamicTaskNameBaseOBJ + obj.name;
  filepath = obj.filepath;
  taskparamscompiled[processname]['-i'] = filepath;
});

console.log('taskparamscompiled:', taskparamscompiled);

Notice that filepath is not declared, and it's redundant. But that is not causing the problem reported:

it's updating all the records with the last instance of the record

Since the code presented works correctly, the problem must lie in some code that has not been provided.

3 Comments

So just to confirm, this should take the appropriate filepath and update the -i value taskpramscompiled?
@JustinMiller Yes, that's what I inferred from what you posted in the question. Run the code snippet to see the output.
I updated what i currently have. i included more of my code. thank you having me look further back into my code.

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.