1

I have two Javascript arrays of objects with various keys/values.

I'm trying to achieve a new array of objects with selected keys/values from each of the original arrays, but have uniqueness on a specific key/value.

Example:

const startDate = [ { name: 'John', //Don't need this Id: 'ae570d88-809b-45b1-bc20-69b569e361ce', //This should be the 'unique' key datePosted: '2020-04-04T00:01:20.000Z' //This will be the start date } ] const endDate = [ { name: 'James', //Don't need this Id: 'ae570d88-809b-45b1-bc20-69b569e361ce', //This should be the 'unique' key datePosted: '2021-04-04T00:01:20.000Z' //This will be the end date } ] const desiredOutput = [ { 'ae570d88-809b-45b1-bc20-69b569e361ce': { startDate: '2020-04-04T00:01:20.000Z', endDate: '2021-04-04T00:01:20.000Z' } } ] const desiredOutput2 = [ { Id: 'ae570d88-809b-45b1-bc20-69b569e361ce', startDate: '2020-04-04T00:01:20.000Z', endDate: '2021-04-04T00:01:20.000Z' } ] 

I've tried using the JS spread operator but can't figure out the renaming of the key to startDate/endDate and adding both to the same object within the array based on uniqueness of the 'Id' key.

Either of the two desiredOutputs would work great

3
  • The left top is for html when you have JS code use the bottom left box in the grid view. Commented Feb 4, 2021 at 9:19
  • 1
    @RokoC.Buljan I'll have various objects in this array. Commented Feb 4, 2021 at 9:21
  • Relevant, but I am not sure it's a full duplicate: How to group an array of objects by key Commented Feb 4, 2021 at 9:26

2 Answers 2

8

You can map over the startDate array and find the endDate based on the Id.

const startDate = [ { name: 'John', //Don't need this Id: 'ae570d88-809b-45b1-bc20-69b569e361ce', //This should be the 'unique' key datePosted: '2020-04-04T00:01:20.000Z' //This will be the start date } ] const endDate = [ { name: 'James', //Don't need this Id: 'ae570d88-809b-45b1-bc20-69b569e361ce', //This should be the 'unique' key datePosted: '2021-04-04T00:01:20.000Z' //This will be the end date } ] const desiredOutput = startDate.map((startObj) => { const foundEndDate = endDate.find((endObj) => endObj.Id === startObj.Id); return { Id: startObj.Id, startDate: startObj.datePosted, endDate: foundEndDate.datePosted, }; }); console.log(desiredOutput);

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

Comments

2

You could take an object with same Id as key and merge the two arrays.

const startDate = [{ name: 'John', Id: 'ae570d88-809b-45b1-bc20-69b569e361ce', datePosted: '2020-04-04T00:01:20.000Z' }], endDate = [{ name: 'James', Id: 'ae570d88-809b-45b1-bc20-69b569e361ce', datePosted: '2021-04-04T00:01:20.000Z' }], result = Object .entries(Object.entries({ startDate, endDate }).reduce((r, [k, v]) => { v.forEach(o => r[o.Id] = { ...r[o.Id], [k]: o.datePosted }) return r; }, {})) .map(a => Object.fromEntries([a])); console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.