forEach loop not updating array as desired

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

consoleOuputParamsOBJ

"outputParameters": [
        {
            "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"
        },

taskparamscompiled updated with current output, it’s updating all the records with the last instance of the record in the consoleOuptutParamsOBJ. I’m trying to get them to update with the correct matching filepath

{
    "process00x00": {
        "-i": "D:\\Code\\UnitTest\\ConsoleApp\\1\\13x23.png",
        "-tr": 16,
        "-tc": 16,
        "-ofr": 16,
        "-ofc": 16,
        "-outfile": "\"D:\\Code\\UnitTest\\ConsoleApp\\Process\\1\""
    },
    "process00x01": {
        "-i": "D:\\Code\\UnitTest\\ConsoleApp\\1\\13x23.png",
        "-tr": 16,
        "-tc": 16,
        "-ofr": 16,
        "-ofc": 16,
        "-outfile": "\"D:\\Code\\UnitTest\\ConsoleApp\\Process\\1\""
    },
    "process00x02": {
        "-i": "D:\\Code\\UnitTest\\ConsoleApp\\1\\13x23.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.

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


    });

37 thoughts on “forEach loop not updating array as desired”

  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 bee provided.

    Reply

Leave a Comment