0

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)

1
  • Don't use for..in when you iterate arrays. Use forEach or map or reduce Commented Dec 17, 2020 at 9:19

4 Answers 4

3

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)

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the comment, one thing is I don't always have assetDetail_ID and supplier_ID in newArr, sometimes it can have more values like asset_Condition or asset_Condition_ID or even other values since the code I post here are not the full object. Is there a way to avoid writing this arr.push({assetDetail_ID: one.assetDetail_ID, supplier_ID: one.supplier_ID}); and instead to auto add objects which are found the same ?
In that case I think you expected result is wrong according to the newArr and oldArr. If you want only the matching keyValue pair then how come the supplier_ID: 5 is there?
I think my logic is wrong abit, thanks for that.
Thanks, that's what I needed
1

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

Comments

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!

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.