需要帮助来修改JS代码以实现所需的输出

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了需要帮助来修改JS代码以实现所需的输出相关的知识,希望对你有一定的参考价值。

我正在编写JS代码,以将我的arr修改为期望的输出。我很近,但无法在arr内打印mfrDispoCodes。以下是我所拥有的:

//常量

var mfrDispoCodes = ['no_mfr_disposition', 'remedied', 
'remedy_in_progress', 'no_action', 'other','with_mfr_notes'];

var codes=['parts_not_avail_vin_confirmed', 'parts_not_avail_disconnect', 'difficulty_obtaining_remedy', 'dealer_sell_through'];

var dispos=[ 'Parts not available VIN confirmed','Parts Not Available Disconnect','Difficulty Obtaining Remedy','Dealer Sell Through'];

//这是我要修改的arr

var arr=[ { voq_disposition: 'Dealer Sell Through',
mfr_disposition: 'Remedied',
cnt: '1',
freq: '100000',
il_cnt: '0',
with_mfr_notes: '0' },
{ voq_disposition: 'Dealer Sell Through',
mfr_disposition: 'Remedy in progress',
cnt: '1',
freq: '100000',
il_cnt: '0',
with_mfr_notes: '0' },

{ voq_disposition: 'Difficulty Obtaining Remedy',
mfr_disposition: 'No Manufacturer disposition',
cnt: '1',
freq: '100000',
il_cnt: '0',
with_mfr_notes: '0' },
{ voq_disposition: 'Difficulty Obtaining Remedy',
mfr_disposition: 'Other',
cnt: '1',
freq: '100000',
il_cnt: '1',
with_mfr_notes: '1' },

{ voq_disposition: 'Parts Not Available Disconnect',
mfr_disposition: 'No Action',
cnt: '1',
freq: '100000',
il_cnt: '0',
with_mfr_notes: '1' },

{ voq_disposition: 'Parts not available VIN confirmed',
mfr_disposition: 'No Action',
cnt: '1',
freq: '100000',
il_cnt: '0',
with_mfr_notes: '1' } ];

//预期结果:因此,基本上不存在freq和count的字段,我仍然希望这些字段为'0'。

{
    parts_not_avail_vin_confirmed: {
      no_mfr_disposition: { freq: '0', count: '0' },
      remedied: { freq: '0', count: '0'},
      remedy_in_progress: { freq: '0', count: '0'},,
      other: { freq: '0', count: '0' },
      no_action: { freq: '100000', count: '1'},
      with_mfr_notes: { freq: '0', count: '1' }
    },
    parts_not_avail_disconnect: {
      no_mfr_disposition: { freq: '0', count: '0' },
      remedied: { freq: '0', count: '0' },
      remedy_in_progress: { freq: '0', count: '0'},,
      other: { freq: '0', count: '0' },
      no_action: { freq: '100000', count: '1' },
      with_mfr_notes: { freq: '0', count: '1' }
    },
    difficulty_obtaining_remedy: {
      no_mfr_disposition: { freq: '100000', count: '1' },
      remedied: { freq: '0', count: '0'},
      remedy_in_progress: { freq: '0', count: '0' },
      other: { freq: '100000', count: '1 1ILs' },
      no_action: { freq: '0', count: '0' },
      with_mfr_notes: { freq: '0', count: '1' }  
    },
    dealer_sell_through: {
      no_mfr_disposition: { freq: '0', count: '0' },
      remedied: { freq: '100000', count: '1' },
      remedy_in_progress: { freq: '100000', count: '1' },
      no_action: { freq: '0', count: '0' },
      with_mfr_notes: { freq: '0', count: '0' }
    }
  }

我正在尝试的内容:

let myObj = {};
for(let i=0;i<arr.length;i++) {
    for(let j=0;j< dispos.length;j++) {
    for(let k=0;k< codes.length;k++) {
  for(let x=0;x<mfrDispoCodes.length;x++){
    if (arr[i].voq_disposition === dispos[j]) {

//if(mfrDispoCodes.indexOf(arr[i].voq_disposition) > -1)        

  console.log('----',mfrDispoCodes[x]);
    myObj[`${codes[k]}`] = {};
    myObj[`${codes[k]}`][`${mfrDispoCodes[x]}`] = {};
 myObj[`${codes[k]}`][`${mfrDispoCodes[x]}`].freq = arr[i].freq;
let ILs;
if(arr[i].il_cnt === '0'){
    ILs= '';
}else{
   ILs = ' '+arr[i].il_cnt+'ILs'
}
 myObj[`${codes[k]}`][`${mfrDispoCodes[x]}`].count = arr[i].cnt+ILs;     
  } 
    else {
    myObj[`${codes[k]}`] = {};
    myObj[`${codes[k]}`][`${mfrDispoCodes[x]}`] = {};

        myObj[`${codes[k]}`][`${mfrDispoCodes[x]}`].freq = '0';
            myObj[`${codes[k]}`][`${mfrDispoCodes[x]}`].count = '0';
  }
}}
}}
console.log(myObj);

//以下是我得到的输出:

{"parts_not_avail_vin_confirmed":{"with_mfr_notes": 
{"freq":"0","count":"0"}},"parts_not_avail_disconnect":{"with_mfr_notes": 
{"freq":"0","count":"0"}},"difficulty_obtaining_remedy": 
{"with_mfr_notes":{"freq":"0","count":"0"}},"dealer_sell_through": 
{"with_mfr_notes":{"freq":"0","count":"0"}}}
答案

我认为这可能是您想要的?我需要为MFR处理全文添加一个常量。

const result = codes.reduce((ret, code, i) => {
  const dispo = dispos[i];
  const matches = arr.filter(e => e.voq_disposition === dispo);

  ret[code] = mfrDispoCodes.reduce((a, m, j) => {
    const mfrDispo = mfrDispos[j];
    const match = matches.find(m => m.mfr_disposition === mfrDispo);

    if(match) {
      a[m] = { count: match.cnt, freq: match.freq };
    } else {
      a[m] = { count: "0", freq: "0" };
    }

    return a;
  }, {});

  return ret;
}, {});

var mfrDispoCodes = [
  "no_mfr_disposition",
  "remedied",
  "remedy_in_progress",
  "no_action",
  "other",
  "with_mfr_notes"
];

var mfrDispos = [
  "No MFR disposition",
  "Remedied",
  "Remedy in progress",
  "No Action",
  "Other",
  "With MFR notes"
];

var codes = [
  "parts_not_avail_vin_confirmed",
  "parts_not_avail_disconnect",
  "difficulty_obtaining_remedy",
  "dealer_sell_through"
];

var dispos = [
  "Parts not available VIN confirmed",
  "Parts Not Available Disconnect",
  "Difficulty Obtaining Remedy",
  "Dealer Sell Through"
];

var arr = [
  {
    voq_disposition: "Dealer Sell Through",
    mfr_disposition: "Remedied",
    cnt: "1",
    freq: "100000",
    il_cnt: "0",
    with_mfr_notes: "0"
  },
  {
    voq_disposition: "Dealer Sell Through",
    mfr_disposition: "Remedy in progress",
    cnt: "1",
    freq: "100000",
    il_cnt: "0",
    with_mfr_notes: "0"
  },
  {
    voq_disposition: "Difficulty Obtaining Remedy",
    mfr_disposition: "No Manufacturer disposition",
    cnt: "1",
    freq: "100000",
    il_cnt: "0",
    with_mfr_notes: "0"
  },
  {
    voq_disposition: "Difficulty Obtaining Remedy",
    mfr_disposition: "Other",
    cnt: "1",
    freq: "100000",
    il_cnt: "1",
    with_mfr_notes: "1"
  },
  {
    voq_disposition: "Parts Not Available Disconnect",
    mfr_disposition: "No Action",
    cnt: "1",
    freq: "100000",
    il_cnt: "0",
    with_mfr_notes: "1"
  },
  {
    voq_disposition: "Parts not available VIN confirmed",
    mfr_disposition: "No Action",
    cnt: "1",
    freq: "100000",
    il_cnt: "0",
    with_mfr_notes: "1"
  }
];

const result = codes.reduce((ret, code, i) => {
  const dispo = dispos[i];
  const matches = arr.filter(e => e.voq_disposition === dispo);
  
  //console.log(matches.filter(m => m.with_mfr_notes === "1").length);

  ret[code] = mfrDispoCodes.reduce((a, m, j) => {
    const mfrDispo = mfrDispos[j];
    const match = matches.find(m => m.mfr_disposition === mfrDispo);
    
    if(match) {
      a[m] = { count: match.cnt, freq: match.freq };
    } else {
      a[m] = { count: "0", freq: "0" };
    }
    
    return a;
  }, {});
  
  ret[code]["with_mfr_notes"] = { 
    count: matches.filter(m => m.with_mfr_notes === "1").length.toString(),
    freq: "0"
  };
  
  return ret;
}, {});

console.log(result);

以上是关于需要帮助来修改JS代码以实现所需的输出的主要内容,如果未能解决你的问题,请参考以下文章

如何修复数据集以返回所需的输出(pytorch)

在循环输出 Oracle PLSQL 函数时需要帮助

对于应对目标攻击前的准备

无法获得所需的掩码检测输出

c++代码执行定时器返回0,需要以毫秒为单位输出

如何显示(输出)在二进制搜索树中查找值所需的迭代次数?