简体   繁体   中英

How to filter data from JS array and create another JS array

I want to filter data from the JS array and create another JS array. I explained the scenario as below.

There are two JS arrays. they are arr1 and arr2

const arr1 = [
{
  code: "XXY",
  dis: "cont1",
  note: "Note for cont1"
}, 
{
  code: "AAW",
  dis: "cont2",
  note: "Note for cont2"
}, 
{
  code: "TTR",
  dis: "cont5",
  note: "Note for cont5"
}, 
{
  code: "MMN",
  dis: "cont10",
  note: "Note for cont10"
}];
const arr2 = [
{
  code: "XXY",
  dis: "cont1",
  note: "Note for cont1"
}, 
{
  code: "AAW",
  dis: "cont2",
  note: "Note for cont2"
}, 
{
  code: "TTR",
  dis: "cont5",
  note: "Note for cont5"
}, 
{
  code: "MMN",
  dis: "cont10",
  note: "Note for cont10"
}, 
{
  code: "vvg",
  dis: "cont15",
  note: "Note for cont15"
}];

we can do change in arr2[1].dis and arr2[2].note and arr2[4].note . I have mentioned it in below

const arr2 = [
{
  code: "XXY",
  dis: "cont1",
  note: "Note for cont1"
}, 
{
  code: "AAW",
  dis: "cont2 new",
  note: "Note for cont2"
}, 
{
  code: "TTR",
  dis: "cont5",
  note: "New Note for cont5"
}, 
{
  code: "MMN",
  dis: "cont10",
  note: "Note for cont10"
}, 
{
  code: "VVG",
  dis: "cont15",
  note: "Note for cont15 changed"
}];

Once I changed the arr2 I want to compare it with arr1 and create new_arr and push all the changed elements to the new_arr . Compare should be done with code . Because code: "VVG" has a change. But it should not be contained a new array. Because code: "VVG" not in arr1 .

Expected-output :

[
  {
    code: "AAW",
    dis: "cont2 new",
    note: "Note for cont2"
  },
  {
    code: "TTR",
    dis: "cont5",
    note: "New Note for cont5"
  }
]

Tried code:

const codesss = new Set(arr1.map(({ code }) => code));
const  new_arr = arr2.filter(({ code }) => !codesss.has(code));

console.log(new_arr);

current output:

[
  {
    "code": "VVG",
    "dis": "cont15",
    "note": "Note for cont15 changed"
  }
]

My current output is not the Expected-output . Please help me to solve this.

First, using Array.prototype.reduce , you can build new object from arr1 with the code variable as key .

Based on that object, you can filter the objects needed using Array.prototype.filter .

 const arr1 = [ { code: "XXY", dis: "cont1", note: "Note for cont1" }, { code: "AAW", dis: "cont2", note: "Note for cont2" }, { code: "TTR", dis: "cont5", note: "Note for cont5" }, { code: "MMN", dis: "cont10", note: "Note for cont10" }]; const arr2 = [ { code: "XXY", dis: "cont1", note: "Note for cont1" }, { code: "AAW", dis: "cont2 new", note: "Note for cont2" }, { code: "TTR", dis: "cont5", note: "New Note for cont5" }, { code: "MMN", dis: "cont10", note: "Note for cont10" }, { code: "VVG", dis: "cont15", note: "Note for cont15 changed" }]; const arr1Obj = arr1.reduce((acc, cur) => { acc[cur.code] = cur; return acc; }, {}); const result = arr2.filter((item) => arr1Obj[item.code] && !(arr1Obj[item.code].dis === item.dis && arr1Obj[item.code].note === item.note)); console.log(result);

 const arr1 = [ { code: "XXY", dis: "cont1", note: "Note for cont1" }, { code: "AAW", dis: "cont2", note: "Note for cont2" }, { code: "TTR", dis: "cont5", note: "Note for cont5" }, { code: "MMN", dis: "cont10", note: "Note for cont10" }]; const arr2 = [ { code: "XXY", dis: "cont1", note: "Note for cont1" }, { code: "AAW", dis: "cont2", note: "Note for cont2" }, { code: "TTR", dis: "cont5", note: "Note for cont5" }, { code: "MMN", dis: "cont10", note: "Note for cont10" }, { code: "vvg", dis: "cont15", note: "Note for cont15" }]; const arr2Changed = [ { code: "XXY", dis: "cont1", note: "Note for cont1" }, { code: "AAW", dis: "cont2 new", note: "Note for cont2" }, { code: "TTR", dis: "cont5", note: "New Note for cont5" }, { code: "MMN", dis: "cont10", note: "Note for cont10" }, { code: "VVG", dis: "cont15", note: "Note for cont15 changed" }]; const arr1Codes = {}; const newArray = []; arr1.map(i => arr1Codes[i.code] = 1); arr2Changed.map((item, index) => { const arr2Item = arr2[index]; if (arr1Codes[arr2Item.code] && (arr2Item.dis !== item.dis || arr2Item.note !== item.note)) { newArray.push(item); } }); console.log(newArray);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM