简体   繁体   中英

Find common values between two array of objects and store as array of objects

I have two array of objects

    const oldArr = [
      {
        assetDetail_ID: 1,
        asset_Condition: "",
        asset_Condition_ID: 0,
        supplier_ID: 5,
      },
      {
        assetDetail_ID: 2,
        asset_Condition: "Good",
        asset_Condition_ID: 3,
        supplier_ID: 10,
      },
    ];

    const newArr = [
      {
        assetDetail_ID: 1,
        supplier_ID: 40,
      },
      {
        assetDetail_ID: 2,
        supplier_ID: 30,
      },
    ];

I am trying find common values by checking with object key and if they are the same, get key and value pair into a new array so my final result will be


   expectedResult = [
      {
        assetDetail_ID: 1,
        supplier_ID: 5,
      },
      {
        assetDetail_ID: 2,
        supplier_ID: 10,
      },
    ];

I have tried this but I am only getting values as [1, 5, 2, 10] and not objects, what am I doing wrong here?

 const oldArr = [{ assetDetail_ID: 1, asset_Condition: "", asset_Condition_ID: 0, supplier_ID: 5, }, { assetDetail_ID: 2, asset_Condition: "Good", asset_Condition_ID: 3, supplier_ID: 10, }, ]; const newArr = [{ assetDetail_ID: 1, supplier_ID: 40, }, { assetDetail_ID: 2, supplier_ID: 30, }, ]; const arr = [] oldArr.forEach((one, x) => { for (let i in one) { for (let j in newArr[x]) if (i === j) { arr.push(one[i]); // If I change to arr.push(one);, it adds the whole object } } }); console.log(arr)

if you want to do it your way,

 const oldArr = [{ assetDetail_ID: 1, asset_Condition: "", asset_Condition_ID: 0, supplier_ID: 5, }, { assetDetail_ID: 2, asset_Condition: "Good", asset_Condition_ID: 3, supplier_ID: 10, }, ]; const newArr = [{ assetDetail_ID: 1, supplier_ID: 40, }, { assetDetail_ID: 2, supplier_ID: 30, }, ]; const arr = oldArr.map((one, index) => { const existingKeys = Object.keys(newArr[index]).filter(key => one.hasOwnProperty(key)); let newObj = existingKeys.reduce((acc, curr) => { acc[curr] = one[curr]; return acc; }, {}); return newObj; }); console.log(arr)

You could take a single loop approach for both arrays and collect all assetDetail_ID of newArr in an object and get a flat array from oldArr by returning either a new object or an empty array.

 const oldArr = [{ assetDetail_ID: 1, asset_Condition: "", asset_Condition_ID: 0, supplier_ID: 5 }, { assetDetail_ID: 2, asset_Condition: "Good", asset_Condition_ID: 3, supplier_ID: 10 }], newArr = [{ assetDetail_ID: 1, supplier_ID: 40 }, { assetDetail_ID: 2, supplier_ID: 30 }], temp = Object.fromEntries(newArr.map(({ assetDetail_ID }) => [assetDetail_ID, true])), result = oldArr.flatMap(({ assetDetail_ID, supplier_ID }) => temp[assetDetail_ID]? { assetDetail_ID, supplier_ID }: [] ); console.log(result);
 .as-console-wrapper { max-height: 100%;important: top; 0; }

function doTheJob(oldArr, newArr){
let result = new Set();
for(let i = 0; i < oldArr.length; i++){
 for(let j = 0; j < newArr.length; j++){
     if(oldArr[i].assertDetail_ID === newArr[j].assertDetail_ID){
         result.add(newArr[j]);
     }
 }
}
return Array.from(result);
}

This can solve your problem!

What you need is classic JavaScript array function filter()

Here's an example -

//your array

const oldArr = [
      {
        assetDetail_ID: 1,
        asset_Condition: "",
        asset_Condition_ID: 0,
        supplier_ID: 5,
      },
      {
        assetDetail_ID: 2,
        asset_Condition: "Good",
        asset_Condition_ID: 3,
        supplier_ID: 10,
      },
    ];

The function -

function getArr(id){
    const newArr = oldArr.filter(obj => { return obj.assetDetail_ID === id})
    console.log(newArr);  //let id = 1; this will give you a sub-array where assetDetail_ID = 1
    return newArr;
}

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