0

How to map an array of objects to another array of objects with multiple key changes? It is given what keys I have to change before the loop starts.

I am doing it using two loops: one for map, and another for key changes.

Example:

var data = [{"result.rows.author_id":1,"result.rows.name":"Ames T","result.rows.orcid":""},{"result.rows.author_id":2,"result.rows.name":"Argasinska J","result.rows.orcid":"0000-0003-2678-2824"}];
var OKeys= ["result.rows.author_id", "result.rows.name", "result.rows.orcid"];
var DKeys= ["id", "name", "orcid"];
var countLength = OKeys.length;

var mappedData = data.map(function(obj) {
  var newObj = {};
  /* How we can remove this loop and still map to new keys */
  for(var i=0;i<countLength;i++) {
      newObj[DKeys[i]] = obj[OKeys[i]];
  }
  return newObj;
});

console.log(mappedData);

I want to know is there any way I can remove countLength loop and still be able to map to new keys or any other way to optimize this two nested loop in one loop.

1
  • 3
    Each array much be iterated over. This is a classic matrix issue. When dealing with a multi-dimensional matrix of values that correspond in some way, each set must be iterated over to determine how that particular set evolves into a different structure. Commented Feb 17, 2021 at 18:21

2 Answers 2

1

If the set of keys to read/write are dynamic, then you'll have to iterate them for each object you want to translate: the number of changes to perform is equal to the number of objects times the number of keys. By consequence you'll have a nested loop.

However, maybe you would like to make the code more functional programming style, so that the inner loop is also a kind of mapper that returns the new object format:

var data = [{"result.rows.author_id":1,"result.rows.name":"Ames T","result.rows.orcid":""},{"result.rows.author_id":2,"result.rows.name":"Argasinska J","result.rows.orcid":"0000-0003-2678-2824"}];
var OKeys= ["result.rows.author_id", "result.rows.name", "result.rows.orcid"];
var DKeys= ["id", "name", "orcid"];

var mappedData = data.map(obj =>
  Object.fromEntries(OKeys.map((k, i) => [DKeys[i], obj[k]]))
);

console.log(mappedData);

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

2 Comments

It is good but are we getting some performance improvement ?
No, that is not expected. As I wrote, the time complexity is already at a minimum. If you have questions specifically about performance, then you should really go to Code Review. But I can already tell you that for performance you should go the other way, and use only standard, old-fashioned for loops -- so no map.
0

Would this work?
If you're just mapping it to new key names.

var data = [{"result.rows.author_id":1,"result.rows.name":"Ames T","result.rows.orcid":""},{"result.rows.author_id":2,"result.rows.name":"Argasinska J","result.rows.orcid":"0000-0003-2678-2824"}];

const test = data.map((obj) => ({
  id: obj["result.rows.author_id"],
  name: obj["result.rows.name"],
  orcid: obj["result.rows.orcid"]
}))

console.log(test)

1 Comment

I am getting source keys and destination keys at runtime and they are dynamic.. I have to fetch using Okeys and Dkeys..

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.