0

I have one object and an array of objects. I want to compare all the objects in the array with the single, hard coded object, and then replace some values where the object properties match.

The single, hardcoded object, describes how a complete dataset should look like when it is correct. Each property has a value of null. This value should be replaced by the value of any object in the array, if the property matches any of the properties in the single one.

// single object (hard coded)

var completeDataSet = {
  jan: null,
  jan;prognosis: null,
  feb: null,
  feb;prognosis: null,
  mar: null,
  mar;prognosis: null,
  apr: null,
  apr;prognosis: null,
  may: null,
  may;prognosis: null,
  jun: null,
  jun;prognosis: null,
  jul: null,
  jul;prognosis: null,
  aug: null,
  aug;prognosis: null,
  sep: null,
  sep;prognosis: null,
  oct: null,
  oct;prognosis: null,
  nov: null,
  nov;prognosis: null,
  dec: null,
  dec;prognosis: null
}

And the array of objects could look something like this:

//Array of objects with missing properties
var data = [{
  jan: 0.258,
  jan;prognosis: false,
  feb: 15.256,
  feb;prognosis: false,
  apr: 0.0156,
  apr;prognosis: true,
  oct: 1.235,
  oct;prognosis: true,
  nov: 3.587,
  nov;prognosis: false
},{
  feb: 12.154,
  feb;prognosis: true,
  apr: 1.015,
  apr;prognosis: true,
  sep: 0.235,
  sep;prognosis: false,
  oct: 15.201,
  oct;prognosis: true,
  nov: 12.158,
  nov;prognosis: false,
  dec: 125.152,
  dec;prognosis: true
}];

I want the end result to look something like this:

var correctArrayOfData = [{
  jan: 0.258,
  jan;prognosis: false,
  feb: 15.256,
  feb;prognosis: false,
  mar: null,
  mar;prognosis: null,
  apr: 0.0156,
  apr;prognosis: true,
  may: null,
  may;prognosis: null,
  jun: null,
  jun;prognosis: null,
  jul: null,
  jul;prognosis: null,
  aug: null,
  aug;prognosis: null,
  sep: null,
  sep;prognosis: null,
  oct: 1.235,
  oct;prognosis: true,
  nov: 3.587,
  nov;prognosis: false,
  dec: null,
  dec;prognosis: null
},{
  jan: null,
  jan;prognosis: null,
  feb: 12.154,
  feb;prognosis: true,
  mar: null,
  mar;prognosis: null,
  apr: 1.015,
  apr;prognosis: true,
  may: null,
  may;prognosis: null,
  jun: null,
  jun;prognosis: null,
  jul: null,
  jul;prognosis: null,
  aug: null,
  aug;prognosis: null,
  sep: 0.235,
  sep;prognosis: false,
  oct: 15.201,
  oct;prognosis: true,
  nov: 12.158,
  nov;prognosis: false,
  dec: 125.152,
  dec;prognosis: true
}]

As you can see i have filled in the values if the key matches, and left the unmatched props and values to be null.

Can you please help me with this one.

Thanks!

1
  • 2
    Welcome to Stack Overflow! Please take the tour and read through the help center, in particular How do I ask a good question? Do your research, search for related topics on SO, and give it a go. If you get stuck and can't get unstuck after doing more research and searching, post a minimal reproducible example of your attempt and say specifically where you're stuck. People will be glad to help. Good luck! Commented Feb 22, 2018 at 11:33

1 Answer 1

2

Use the expected result as template and then assign the attributes to the array correctArrayOfData.

var data = [{  jan: 0.258,  'jan;prognosis': false,  feb: 15.256,  'feb;prognosis': false,  apr: 0.0156,  'apr;prognosis': true,  oct: 1.235,  'oct;prognosis': true,  nov: 3.587,  'nov;prognosis': false},{  feb: 12.154,  'feb;prognosis': true,  apr: 1.015,  'apr;prognosis': true,  sep: 0.235,  'sep;prognosis': false,  oct: 15.201,  'oct;prognosis': true,  nov: 12.158,  'nov;prognosis': false,  dec: 125.152,  'dec;prognosis': true}];
var template = {  jan: null,  'jan;prognosis': null,  feb: null,  'feb;prognosis': null,  mar: null,  'mar;prognosis': null,  apr: null,  'apr;prognosis': null,  may: null,  'may;prognosis': null,  jun: null,  'jun;prognosis': null,  jul: null,  'jul;prognosis': null,  aug: null,  'aug;prognosis': null,  sep: null,  'sep;prognosis': null,  oct: null,  'oct;prognosis': null,  nov: null,  'nov;prognosis': null,  dec: null,  'dec;prognosis': null};

var correctArrayOfData = data.map(o => Object.assign(Object.assign({}, template), o));
console.log(correctArrayOfData);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

2 Comments

Thank you for the response! I came up with a similar approach but forgot to assign the template to a new object.
@AntonBollenForsberg you're welcome! btw, I've updated the answer with assign.

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.