0

I have an input array of object and a desired/output array

Input Array of object

var input = [{
"Grade": "AU27",
"Thickness10Qty": "7.00",
"Thickness5Qty": "19.20",
"TotalQty": "26.20"
},
{
"Grade": "FE500D",
"Thickness10Qty": "143.00",
"Thickness12Qty": "69.00",
"Thickness8Qty": "30.00",
"TotalQty": "242.00"
}, 
{
"Grade": "GE500D",
"Thickness18Qty": "90.00",
"Thickness22Qty": "40.00",
"TotalQty": "130.00"
}, 
{
"Grade": "HE500D",
"Thickness26Qty": "70.00",
"TotalQty": "70.00"
}
];

I have this as the desired array of object

var output = [{
 "Grade": "AU27",
 "Thickness10Qty": "7.00",
 "Thickness5Qty": "19.20",
 "Thickness12Qty": "0.00",
 "Thickness8Qty": "0.00",
 "Thickness18Qty": "0.00",
 "Thickness22Qty": "0.00",
 "Thickness26Qty": "0.00",
 "TotalQty": "26.20"
 },
 {
  "Grade": "FE500D",
  "Thickness10Qty": "143.00",
  "Thickness12Qty": "69.00",
  "Thickness8Qty": "30.00",
  "Thickness5Qty": "0.00",
  "Thickness18Qty": "0.00",
  "Thickness22Qty": "0.00",
  "Thickness26Qty": "0.00",
  "TotalQty": "242.00"
 }, 
 {
  "Grade": "GE500D",
  "Thickness18Qty": "90.00",
  "Thickness22Qty": "40.00",
  "Thickness10Qty": "0.00",
  "Thickness12Qty": "0.00",
  "Thickness8Qty": "0.00",
  "Thickness5Qty": "0.00",
  "Thickness26Qty": "0.00",
  "TotalQty": "130.00"
  }, 
  {
   "Grade": "HE500D",
   "Thickness26Qty": "70.00",
   "Thickness18Qty": "0.00",
   "Thickness22Qty": "0.00",
   "Thickness10Qty": "0.00",
   "Thickness12Qty": "0.00",
   "Thickness8Qty": "0.00",
   "Thickness5Qty": "0.00",
    "TotalQty": "70.00"
   }
  ];

What I have to do is that suppose in the first object in the input array of object, only Thickness10Qty and Thickness5Qty, so I have to enter other keys as 0. I want to have equal number of keys in every objects. Similarly for the last object in the input array of object, only Thickness26Qty exists. So we have insert other keys as 0 to have equal number of keys in every objects.

How can I achieve this desired array of object with equal number of keys?

The keys cannot be hard coded, it is coming from a service, the service may return Thickness34Qty/Thickness46Qty instead of Thickness22Qty or Thickness38Qty instead of Thickness18Qty

1
  • Post JavaScript as a minimal reproducible example -- specifically the function(s) and/or expression, and/or statements...basically some code that actually does something (or tries to despite errors). Commented Apr 18, 2019 at 4:04

4 Answers 4

1

You could make a template object that has all the keys set to 0.00 which defines which fields you want, then you can use Object.assign() inside map() to merge this with your existing object.

var input = [{"Grade": "AU27","Thickness10Qty": "7.00","Thickness5Qty": "19.20","TotalQty": "26.20"},{"Grade": "FE500D","Thickness10Qty": "143.00","Thickness12Qty": "69.00","Thickness8Qty": "30.00","TotalQty": "242.00"}, {"Grade": "GE500D","Thickness18Qty": "90.00","Thickness22Qty": "40.00","TotalQty": "130.00"}, {"Grade": "HE500D","Thickness26Qty": "70.00","TotalQty": "70.00"}];

// make template of all keys
let template  = input.reduce((obj, item) => (Object.keys(item).forEach(k => obj[k] = '0.00'), obj), {})

// apply to items:
let newArray = input.map(item => Object.assign({}, template, item))

console.log(newArray)

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

Comments

1

Make an array of all the keys that should be in an object, then iterate through the array of objects, and add any keys that are missing, with the value 0.00:

var input = [{
    "Grade": "AU27",
    "Thickness10Qty": "7.00",
    "Thickness5Qty": "19.20",
    "TotalQty": "26.20"
  },
  {
    "Grade": "FE500D",
    "Thickness10Qty": "143.00",
    "Thickness12Qty": "69.00",
    "Thickness8Qty": "30.00",
    "TotalQty": "242.00"
  },
  {
    "Grade": "GE500D",
    "Thickness18Qty": "90.00",
    "Thickness22Qty": "40.00",
    "TotalQty": "130.00"
  },
  {
    "Grade": "HE500D",
    "Thickness26Qty": "70.00",
    "TotalQty": "70.00"
  }
];

var keys = ["Grade", "TotalQty", "Thickness5Qty", "Thickness8Qty", "Thickness10Qty", "Thickness12Qty", "Thickness18Qty", "Thickness22Qty", "Thickness26Qty"];

var output = input.map(obj => {
  keys.forEach(key => obj[key] ? key : obj[key] = "0.00");
  return obj;
});

console.log(output);
.as-console-wrapper { max-height: 100% !important; top: auto; }

If the keys are variable (e.g. you want every object to have the same keys):

var input = [{
    "Grade": "AU27",
    "Thickness10Qty": "7.00",
    "Thickness5Qty": "19.20",
    "TotalQty": "26.20"
  },
  {
    "Grade": "FE500D",
    "Thickness10Qty": "143.00",
    "Thickness12Qty": "69.00",
    "Thickness8Qty": "30.00",
    "TotalQty": "242.00"
  },
  {
    "Grade": "GE500D",
    "Thickness18Qty": "90.00",
    "Thickness22Qty": "40.00",
    "TotalQty": "130.00"
  },
  {
    "Grade": "HE500D",
    "Thickness26Qty": "70.00",
    "TotalQty": "70.00"
  }
];

var keys = [...new Set(input.map(Object.keys).reduce((acc, curr) => acc.concat(curr)))];

var output = input.map(obj => {
  keys.forEach(key => obj[key] ? key : obj[key] = "0.00");
  return obj;
});

console.log(output);
.as-console-wrapper { max-height: 100% !important; top: auto; }

7 Comments

the keys cannot be hard coded, it is coming from a service, the service may return Thickness34Qty instead of Thickness22Qty
Then just replace keys with the keys you need, in the format of [keyName].
Can you please recheck the solution
Can you please modify the fiddle and kindly demonstrate?
nice +1: love that map(Object.keys)
|
0

This may not set any performance records, but should demonstrate the steps that occur and how it can be slightly optimized. It's important to note that operating on the objects will mutate the objects in the input array, so there is no input/output array as that would require cloning the objects and more work than is required.

let array = getData()
normalizeData(array)
console.log('output:', array)

function normalizeData(array) {
  let stored_keys = {}
  
  array.forEach((obj, ndx, arr) => {
    let new_keys = []

    // create keys found in other objects
    Object.keys(stored_keys).forEach(key => {
      if (!(key in obj))
        obj[key] = "0.00"
    })

    // detect new keys
    Object.keys(obj).forEach(key => {
      if (!stored_keys[key]) {
        stored_keys[key] = true // track key
        new_keys.push(key)      // track new keys to apply to previous objects
      }
    })

    // add any new keys to previous objects
    if (new_keys.length)
      for (var i = 0, n = ndx; i < n; i++) {
        let prev_obj = arr[i];
        new_keys.forEach(key => prev_obj[key] = "0.00")
      }

  })
}


function getData() {
  return [{
      "Grade": "AU27",
      "Thickness10Qty": "7.00",
      "Thickness5Qty": "19.20",
      "TotalQty": "26.20"
    },
    {
      "Grade": "FE500D",
      "Thickness10Qty": "143.00",
      "Thickness12Qty": "69.00",
      "Thickness8Qty": "30.00",
      "TotalQty": "242.00"
    },
    {
      "Grade": "GE500D",
      "Thickness18Qty": "90.00",
      "Thickness22Qty": "40.00",
      "TotalQty": "130.00"
    },
    {
      "Grade": "HE500D",
      "Thickness26Qty": "70.00",
      "TotalQty": "70.00"
    }
  ];
}

Comments

0

I think @mark-meyer is on the money with this one. I'll just offer a slightly more readable version.

    const baseObject = {
      "Thickness10Qty": "0.00",
      "Thickness5Qty": "0.00",
      "Thickness12Qty": "0.00",
      "Thickness8Qty": "0.00",
      "Thickness18Qty": "0.00",
      "Thickness22Qty": "0.00",
      "Thickness26Qty": "0.00",
      "TotalQty": "0.00"
    };

    const addMissingvalues = arr => arr.map(obj => ({
      ...baseObject,
      ...obj,
    }));

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.