Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/454.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 比较两个数组并分组为插入和更新_Javascript_Arrays - Fatal编程技术网

Javascript 比较两个数组并分组为插入和更新

Javascript 比较两个数组并分组为插入和更新,javascript,arrays,Javascript,Arrays,我比较两个数组,然后将它们分组为插入和更新。更新数组应该最终包含在id==id上匹配的元素,但是open\u balance!-文档装载。inserts数组应仅包含id!=Id。请注意,我正在通过toString()方法运行这两个函数,因为在我的场景中,一个函数的类型为String,而另一个函数的类型为Integer,来自两个不同的SQL数据库 现在,使用我现在拥有的代码,第一个分组起作用-通过findRecordsToUpdate()函数,我得到了所有与更新正确匹配的元素-应该是两个元素,因为

我比较两个数组,然后将它们分组为插入和更新。更新数组应该最终包含在
id==id
上匹配的元素,但是
open\u balance!-文档装载
。inserts数组应仅包含
id!=Id
。请注意,我正在通过
toString()
方法运行这两个函数,因为在我的场景中,一个函数的类型为String,而另一个函数的类型为Integer,来自两个不同的SQL数据库

现在,使用我现在拥有的代码,第一个分组起作用-通过
findRecordsToUpdate()
函数,我得到了所有与更新正确匹配的元素-应该是两个元素,因为这两个元素在id上匹配,但在数量上不匹配:

  recordsToUpdate: [
   { Id: '333', DocumentAmount: 20 },
   { Id: '444', DocumentAmount: 10 }
  ] 
但是由
findRecordsToInsert()
函数生成的插入不正确。根据我的数据,我应该以一个元素结束,在我的
recordsToUpdate
数组中Id为“555”,因为它是唯一一个Id在另一个数组中找不到的元素,也就是说,它是要插入的新记录。但是我现在的代码却有13个元素

我在这里遗漏了什么?为了将正确的插入填充到我的recordsToInsert数组,我需要更改什么

const sourceArr = [
  { id: 111, open_balance: 10 },
  { id: 222, open_balance: 20 },
  { id: 333, open_balance: 10 },
  { id: 444, open_balance: 20 },
]

const targetArr = [
  { Id: '111', DocumentAmount: 10 },
  { Id: '222', DocumentAmount: 20 },
  { Id: '333', DocumentAmount: 20 },
  { Id: '444', DocumentAmount: 10 },
  { Id: '555', DocumentAmount: 10 },
]

function findRecordsToUpdate () {
  if (sourceArr.length && targetArr.length) {
    let recordsToUpdate = [];
    for (let t of targetArr) {
      for (let s of sourceArr) {
        if ((t.Id.toString() == s.id.toString()) && (t.DocumentAmount != s.open_balance)) {
          recordsToUpdate.push(t);
        }
      }
    }
    console.log('recordsToUpdate: ', recordsToUpdate);
    return recordsToUpdate;
  }
};

function findRecordsToInsert () {
  if (sourceArr.length && targetArr.length) {
    let recordsToInsert = [];
    for (let t of targetArr) {
      for (let s of sourceArr) {
        if (t.Id.toString() != s.id.toString()) {
          recordsToInsert.push(t);
        }
      }
    }
    console.log('recordsToInsert: ', recordsToInsert);
    return recordsToInsert;
  }
};

findRecordsToUpdate();
findRecordsToInsert();
顺便说一句,为了澄清这一点,我的
findRecordsToInsert()
函数在完成后应该返回一个
recordsToInsert
数组,因为Id为555的记录是第二个数组中第一个数组中不存在的唯一记录:

recordsToInsert: [{ Id: '555', DocumentAmount: 10 }]

因此,需要明确的是,目前只有
findRecordsToInsert()
函数工作不正常。

使用
Array#filter
Array#find
的组合,从
targetar
中获取插入和更新数组

“严格使用”;
常量sourceArr=[
{id:111,未结余额:10},
{id:222,未结余额:20},
{id:333,未结余额:10},
{id:444,未结余额:20},
];
常量目标=[
{Id:'111',mount:10},
{Id:'222',挂载量:20},
{Id:'333',挂载量:20},
{Id:'444',挂载:10},
{Id:'555',documentmount:10},
];
//金额已更改且具有匹配源(在ID上)的目标记录。
//更新这些。
const recordsToUpdate=targetar
.filter(tar=>sourceArr
.find(source=>source.id==Number(tar.id)&&source.open\u balance!==tar.documentmount));
console.log(recordsToUpdate);
//没有匹配源记录的目标记录(即新记录)。
//插入这些。
const recordsToInsert=targetar
.filter(tar=>!sourceArr
.find(source=>source.id==Number(tar.id));

console.log(recordsToInsert)您可以获取一个哈希表并只筛选目标数组

const
sourceArr=[{id:111,未清余额:10},{id:222,未清余额:20},{id:333,未清余额:10},{id:444,未清余额:20}],
targetar=[{Id:'111',documentmount:10},{Id:'222',documentmount:20},{Id:'333',documentmount:20},{Id:'444',documentmount:10},{Id:'555',documentmount:10}],
source=sourceArr.reduce((r,{id,open_balance})=>(r[id]=open_balance,r),{}),
recordsToUpdate=targetar.filter({Id,documentmount})=>
源中的Id&&source[Id]!==DocumentMount),
recordsToInsert=targetar.filter(({Id})=>!(源中的Id));
console.log(recordsToUpdate);
console.log(recordsToInsert)

。作为控制台包装{max height:100%!important;top:0;}
您有结果吗?顺便说一句,为什么不使用相同的id类型?因为在我们的现实场景中,这两个类型不是相同的(我没有设置它们)。我将在上面添加我想要的结果。这非常有效,而且更加简洁。谢谢,@avejidah!当然,很乐意帮忙。