forEach循环未根据需要更新数组 [英] forEach loop not updating array as desired

查看:81
本文介绍了forEach循环未根据需要更新数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图遍历consoleOuputParamsOBJ并更新我的taskparams编译对象列表中的记录

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

所需的输出

    {
    "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\""
    }

我当前正在做的事情,进程名似乎正在工作,因为它正在更新该值,但是用于更新它的数据只是taskparamscompiled数据集中的最后一条记录.

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.

当前代码更新:

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);

推荐答案

发生此问题是因为您要使用以下这一行将同一对象的副本分配给 taskparamscompiled 中的每个键:

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

您需要复制对象,您可以使用 Object.assign .您可以在构建 taskparamscompiled 对象的同时,使用它来更新输入文件名( -i 属性):

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);

这篇关于forEach循环未根据需要更新数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆