Home > Software design >  How can I merge value from one object to an element in an array of objects?
How can I merge value from one object to an element in an array of objects?

Time:11-22

I have two arrays--one of strings and one of objects. I find duplicates in the first array and get a count. I want to add the integer value i found in the counts object to the pbentry using the Product2Id as the key. I honestly haven't been able to figure it out and hours of google-fu has resulted in nothing.

but the amount of those id values in count will always be equal to the amount of product2Id in pbentry.

Desired outcome:

[{"Id":"01u8D00000105oqQAA","Product2Id":"01t8D000001fDfjQAE","Count":"3"},
{"Id":"01u8D00000105oxQAA","Product2Id":"01t8D000001fDfqQAE","Count":"1"},
{"Id":"01u8D00000105p2QAA","Product2Id":"01t8D000001fDfvQAE","Count":"1"},
{"Id":"01u8D000003WBH5QAO","Product2Id":"01t1O000004XyR0QAK","Count":"2"},
{"Id":"01u8D000003WBH0QAO","Product2Id":"01t8D000001hKF1QAM","Count":"1"}....];

let counts = {}; 
let array = ["01t8D0000014jiuQAA", "01t5Y000006VydJQAS", "01t8D000001fDfjQAE", "01t8D000001fDfjQAE", "01t8D000001hKF1QAM", "01t1O000004XyR0QAK", "01t14000006956yAAA", "01t1O000004XyR0QAK", "01t8D000001fDfqQAE", "01t8D000001f1yeQAA", "01t8D000001fDfvQAE", "01t8D000001fDfjQAE"];
let pbentry = [{"Id":"01u8D000003WBHAQA4","Product2Id":"01t14000006956yAAA"},{"Id":"01u8D000003WBH5QAO","Product2Id":"01t1O000004XyR0QAK"}, {"Id":"01u8D000000zEfiQAE","Product2Id":"01t5Y000006VydJQAS"},{"Id":"01u8D000003WBGqQAO","Product2Id":"01t8D0000014jiuQAA"},
{"Id":"01u8D000003WBHyQAO","Product2Id":"01t8D000001f1yeQAA"},{"Id":"01u8D00000105oqQAA","Product2Id":"01t8D000001fDfjQAE"}, {"Id":"01u8D00000105oxQAA","Product2Id":"01t8D000001fDfqQAE"},{"Id":"01u8D00000105p2QAA","Product2Id":"01t8D000001fDfvQAE"}, {"Id":"01u8D000003WBH0QAO","Product2Id":"01t8D000001hKF1QAM"}];

array.forEach(function (x) { counts[x] = (counts[x] || 0)   1; }); 

console.log(pbentry)

CodePudding user response:

This line should do it, it just gets the count you've already worked out and attaches it to your pbentry array.:

pbentry = pbentry.map(pb => ({...pb, Count: counts[pb.Product2Id] || 0}))

Add it after you've worked out your counts:

let counts = {}; 
let array = ["01t8D0000014jiuQAA", "01t5Y000006VydJQAS", "01t8D000001fDfjQAE", "01t8D000001fDfjQAE", "01t8D000001hKF1QAM", "01t1O000004XyR0QAK", "01t14000006956yAAA", "01t1O000004XyR0QAK", "01t8D000001fDfqQAE", "01t8D000001f1yeQAA", "01t8D000001fDfvQAE", "01t8D000001fDfjQAE"];
let pbentry = [{"Id":"01u8D000003WBHAQA4","Product2Id":"01t14000006956yAAA"},{"Id":"01u8D000003WBH5QAO","Product2Id":"01t1O000004XyR0QAK"}, {"Id":"01u8D000000zEfiQAE","Product2Id":"01t5Y000006VydJQAS"},{"Id":"01u8D000003WBGqQAO","Product2Id":"01t8D0000014jiuQAA"},
{"Id":"01u8D000003WBHyQAO","Product2Id":"01t8D000001f1yeQAA"},{"Id":"01u8D00000105oqQAA","Product2Id":"01t8D000001fDfjQAE"}, {"Id":"01u8D00000105oxQAA","Product2Id":"01t8D000001fDfqQAE"},{"Id":"01u8D00000105p2QAA","Product2Id":"01t8D000001fDfvQAE"}, {"Id":"01u8D000003WBH0QAO","Product2Id":"01t8D000001hKF1QAM"}];

array.forEach(function (x) { counts[x] = (counts[x] || 0)   1; }); 

pbentry = pbentry.map(pb => ({...pb, Count: counts[pb.Product2Id] || 0}))

console.log(pbentry)

CodePudding user response:

Presented below is one possible way to achieve the desired objective.

Code Snippet

const myTransform = (countArr, infoArr) => (
  // iterate over the infoArr
  infoArr?.map(({Product2Id, ...rest}) => ({
    // populate Product2Id & other fields as-is
    ...rest, Product2Id,
    // set-up "Count" using "countArr" (by filtering for particular Product2Id
    // and getting the length of the filtered array)
    Count: countArr?.filter(x => x === Product2Id)?.length,
  }))
);

let counts = {};
let myArray = ["01t8D0000014jiuQAA", "01t5Y000006VydJQAS", "01t8D000001fDfjQAE", "01t8D000001fDfjQAE", "01t8D000001hKF1QAM",
  "01t1O000004XyR0QAK", "01t14000006956yAAA", "01t1O000004XyR0QAK", "01t8D000001fDfqQAE", "01t8D000001f1yeQAA",
  "01t8D000001fDfvQAE", "01t8D000001fDfjQAE"
];


let pbentry = [{
    "Id": "01u8D000003WBHAQA4",
    "Product2Id": "01t14000006956yAAA"
  }, {
    "Id": "01u8D000003WBH5QAO",
    "Product2Id": "01t1O000004XyR0QAK"
  },
  {
    "Id": "01u8D000000zEfiQAE",
    "Product2Id": "01t5Y000006VydJQAS"
  }, {
    "Id": "01u8D000003WBGqQAO",
    "Product2Id": "01t8D0000014jiuQAA"
  },
  {
    "Id": "01u8D000003WBHyQAO",
    "Product2Id": "01t8D000001f1yeQAA"
  }, {
    "Id": "01u8D00000105oqQAA",
    "Product2Id": "01t8D000001fDfjQAE"
  },
  {
    "Id": "01u8D00000105oxQAA",
    "Product2Id": "01t8D000001fDfqQAE"
  }, {
    "Id": "01u8D00000105p2QAA",
    "Product2Id": "01t8D000001fDfvQAE"
  },
  {
    "Id": "01u8D000003WBH0QAO",
    "Product2Id": "01t8D000001hKF1QAM"
  }
];

console.log(
  'counting unique ids:\n',
  myTransform(myArray, pbentry)
);
.as-console-wrapper {
  max-height: 100% !important;
  top: 0
}

Explanation

Inline comments added to the snippet above.

  • Related