0

I have two arrays as follows,

columnNames = ['KEY_ID', 'SOURCE_ID', 'MTNAME', 'MTNO', 'PLANTS']

columnValues = [{col0:"719801", col1: "4198", col2: "010", col3: "200887", col4: "6LLA"}, {col0:"719901", col1: "5198", col2: "011", col3: "207887", col4: "6PPA"}]

My expected output is,

outputArray = [{KEY_ID:"719801", SOURCE_ID: "4198", MTNAME: "010", MTNO: "200887", PLANTS: "6LLA"}, {KEY_ID:"719901", SOURCE_ID: "5198", MTNAME: "011", MTNO: "207887", PLANTS: "6PPA"}]

The number of items and contents of both arrays change dynamically i.e., columnNames can have different names and columnValues can have any number of values. Any inputs to obtain the final outputArray will be highly helpful. I have tried using reduce. It either requires multiple iterations or the columnNames array to be constant.

let renameKeys = (columnValues, object) =>
    Object.keys(object).reduce(
      (acc, key) => ({
        ...acc,
        ...{ [columnValues[key] || key]: object[key] },
      }),
      {}
    );        

 console.log(renameKeys(columnNames,columnValues))

5
  • Please show the output when there are more columnNames than columnValues and vice versa Commented Nov 29, 2022 at 18:43
  • You do not have an object, you have a weird array. I made a snippet and changed the columnValues to an object Commented Nov 29, 2022 at 18:46
  • should columnValues be a json object? It is in square brackets, like an array, but contains key/value pairs, like an object. Commented Nov 29, 2022 at 18:46
  • columnValues is an array of such objects. I am adding more values to clear the picture. Commented Nov 30, 2022 at 10:30
  • @MaruthiRevankar I added an answer, Hope it will work as per your expectation. Commented Nov 30, 2022 at 14:18

3 Answers 3

1

Your expected output array is not valid. It should be an array of objects. You can achieve this requirement by iterating the input array with the help of Array.forEach().

Live Demo :

const columnNames = ['KEY_ID', 'SOURCE_ID', 'MTNAME', 'MTNO', 'PLANTS'];

const columnValues = [{col0:"719801", col1: "4198", col2: "010", col3: "200887", col4: "6LLA"}, {col0:"719901", col1: "5198", col2: "011", col3: "207887", col4: "6PPA"}];

columnValues.forEach(obj => {
    Object.keys(obj).forEach((key, index) => {
    obj[columnNames[index]] = obj[key]
    delete obj[key];
  });
});

console.log(columnValues);

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

Comments

1

This will work for you !

let columnNames = ['KEY_ID', 'SOURCE_ID', 'MTNAME', 'MTNO', 'PLANTS']
let columnValues = [{col0:"719801", col1: "4198", col2: "010", col3: "200887", col4: "6LLA"}, {col0:"719901", col1: "5198", col2: "011", col3: "207887", col4: "6PPA"}]

renameKey = (obj, old_key, new_key) => {   
          // check if old key = new key  
              if (old_key !== new_key) {                  
                 Object.defineProperty(obj, new_key, // modify old key
                                      // fetch description from object
                 Object.getOwnPropertyDescriptor(obj, old_key));
                 delete obj[old_key];                // delete old key
                 }
          }

columnValues.map((cv)=>{
Object.keys(cv).map((keys,index)=>{
   renameKey(cv,keys,columnNames[index])
  })
})
console.log(columnValues)

Comments

0

Your columnValues is not an object as it has square brackets

This will ONLY work if there are as many names as values:

const columnNames = ['KEY_ID', 'SOURCE_ID', 'MTNAME', 'MTNO', 'PLANTS'],
columnValues = {col0:"719801", col1: "4198", col2: "010", col3: "200887", col4: "6LLA"}
let renameKeys = (columnValues, object) => 
  Object.values(object).reduce((acc, val, i) => {
    acc[columnNames[i]] = val;
    return acc}, 
  {});


console.log(renameKeys(columnNames, columnValues))

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.