简体   繁体   English

查找两个对象数组之间的共同值并存储为对象数组

[英]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我正在尝试通过检查 object key来找到共同值,如果它们相同,则将键和值对放入一个新数组中,这样我的最终结果将是


   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?我已经尝试过了,但我只得到[1, 5, 2, 10]的值而不是对象,我在这里做错了什么?

 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.您可以对 arrays 采取单循环方法,并在 object 中收集 newArr 的所有assetDetail_ID ,并通过返回新的newArr或空数组从oldArr获取平面数组。

 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()您需要的是经典 JavaScript 阵列 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 -

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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